Form表单
rules格式校验
可以在validator中进行网络请求,实现网络校验
const formRules = { userName: [{required: true, message: "用户名不能为空", trigger: 'blur'}, { min: 5, max: 10, message: "长度必须5-10位", trigger: 'blur' }], passWord: [{required: true, message: "密码不能为空", trigger: 'blur'}], reference: [{ validator: referenceValidity, required: true, trigger: "blur", },], } function referenceValidity(rule, value, callback) { console.log('校验推荐人') if (value === "" || value === undefined) { callback(new Error("请输入推荐人姓名")); } else { //请求网络校验推广人 // console.log("输入的推荐人姓名:" + value) // const _url = "xxxxxxxxxxx"; // let param = {}; // param.promotionPecialistName = value; // get(this.$http, _url, param).then(function (response) { // let res = response.data; // if (res.code === "I000000") { // if (res.data.isFlag) { // callback() // } else { // callback(new Error("推荐人不存在")) // } // } else { // callback(new Error("推荐人不存在")) // } // }); callback(new Error("推荐人不存在")) } }
formRef引用form对象 form表单中关联formRef(引用对象),form(表单内容),formRules(校验规则)
const formRef = ref() function reqLogin() { console.log("账号信息", form.value) formRef.value.validate((valid) => { console.log("formRef valid = ", valid) if (valid) { setToken() store.commit("setUserInfo", form.value) showToast("登录成功") router.push('/') } }) }
键盘监听
onMounted(() => { console.log("页面加载") document.addEventListener('keyup', onkeyup) }) onBeforeUnmount(() => { document.removeEventListener('keyup', onkeyup) }) function onkeyup(e) { if (e.key === 'Enter') { reqLogin() } }
完整代码
import {onBeforeUnmount, onMounted, ref} from "vue"; import {Lock, User} from "@element-plus/icons-vue"; import {useRouter} from "vue-router"; import {setToken} from "../utils/CookieUtil.js"; import {showToast} from "../utils/ToastUtil.js"; import {useStore} from "vuex"; const router = useRouter() const store = useStore() const form = ref({ userName: "", passWord: "", reference: "" }) const formRef = ref() const formRules = { userName: [{required: true, message: "用户名不能为空", trigger: 'blur'}, { min: 5, max: 10, message: "长度必须5-10位", trigger: 'blur' }], passWord: [{required: true, message: "密码不能为空", trigger: 'blur'}], reference: [{ validator: referenceValidity, required: true, trigger: "blur", },], } function referenceValidity(rule, value, callback) { console.log('校验推荐人') if (value === "" || value === undefined) { callback(new Error("请输入推荐人姓名")); } else { //请求网络校验推广人 // console.log("输入的推荐人姓名:" + value) // const _url = "xxxxxxxxxxx"; // let param = {}; // param.promotionPecialistName = value; // get(this.$http, _url, param).then(function (response) { // let res = response.data; // if (res.code === "I000000") { // if (res.data.isFlag) { // callback() // } else { // callback(new Error("推荐人不存在")) // } // } else { // callback(new Error("推荐人不存在")) // } // }); callback(new Error("推荐人不存在")) } } function onkeyup(e) { if (e.key === 'Enter') { reqLogin() } } onMounted(() => { console.log("页面加载") document.addEventListener('keyup', onkeyup) }) onBeforeUnmount(() => { document.removeEventListener('keyup', onkeyup) }) function reqLogin() { console.log("账号信息", form.value) formRef.value.validate((valid) => { console.log("formRef valid = ", valid) if (valid) { setToken() store.commit("setUserInfo", form.value) showToast("登录成功") router.push('/') } }) }.flex-col-center { display: flex; flex-direction: column; justify-content: center }测试系统登录页面 演示Demo欢迎回来
登录
到此这篇关于Vue Form表单的使用,rules格式校验网络校验,键盘按键监听的文章就介绍到这了,更多相关Vue Form表单rules格式内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!