Vue Form表單的使用方法(rules格式校驗(yàn)網(wǎng)絡(luò)校驗(yàn)鍵盤按鍵監(jiān)聽)
Form表單

rules格式校驗(yàn)
可以在validator中進(jìn)行網(wǎng)絡(luò)請求,實(shí)現(xiàn)網(wǎng)絡(luò)校驗(yàn)
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('校驗(yàn)推薦人')
if (value === "" || value === undefined) {
callback(new Error("請輸入推薦人姓名"));
} else {
//請求網(wǎng)絡(luò)校驗(yàn)推廣人
// 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表單中關(guān)聯(lián)formRef(引用對象),form(表單內(nèi)容),formRules(校驗(yàn)規(guī)則)
<el-form ref="formRef" :model="form" :rules="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('/')
}
})
}鍵盤監(jiān)聽
onMounted(() => {
console.log("頁面加載")
document.addEventListener('keyup', onkeyup)
})
onBeforeUnmount(() => {
document.removeEventListener('keyup', onkeyup)
})
function onkeyup(e) {
if (e.key === 'Enter') {
reqLogin()
}
}完整代碼
<script setup>
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('校驗(yàn)推薦人')
if (value === "" || value === undefined) {
callback(new Error("請輸入推薦人姓名"));
} else {
//請求網(wǎng)絡(luò)校驗(yàn)推廣人
// 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('/')
}
})
}
</script>
<template>
<div style="height: 100vh;width: 100vw;display: flex;flex-direction: row">
<div class="flex-col-center" style="width: 70%;height: 100%;background: #213547;">
<span style="font-size: 40px;color: white;">測試系統(tǒng)登錄頁面</span>
<span style="font-size: 20px;color: white;">演示Demo</span>
</div>
<div class="flex-col-center" style="width: 30%;padding: 100px">
<h2 style="font-size: 20px">歡迎回來</h2>
<el-form ref="formRef" :model="form" :rules="formRules">
<el-form-item label="賬號" prop="userName">
<el-input v-model="form.userName" placeholder="請輸入賬號">
<template #prefix>
<el-icon>
<User/>
</el-icon>
</template>
</el-input>
</el-form-item>
<el-form-item label="密碼" prop="passWord">
<el-input v-model="form.passWord" placeholder="請輸入密碼" show-password type="password">
<template #prefix>
<el-icon>
<Lock/>
</el-icon>
</template>
</el-input>
</el-form-item>
<el-form-item label="推薦人" prop="reference">
<el-input v-model="form.reference" placeholder="請輸入推薦人"></el-input>
</el-form-item>
</el-form>
<el-button type="primary" @click="reqLogin()">登錄</el-button>
</div>
</div>
</template>
<style scoped>
.flex-col-center {
display: flex;
flex-direction: column;
justify-content: center
}
</style>到此這篇關(guān)于Vue Form表單的使用,rules格式校驗(yàn)網(wǎng)絡(luò)校驗(yàn),鍵盤按鍵監(jiān)聽的文章就介紹到這了,更多相關(guān)Vue Form表單rules格式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3中關(guān)于i18n字符串轉(zhuǎn)義問題
這篇文章主要介紹了vue3中關(guān)于i18n字符串轉(zhuǎn)義問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04
解決VUE3 keep-alive頁面切換報錯parentComponent.ctx.deactivate
這篇文章主要介紹了解決VUE3 keep-alive頁面切換報錯parentComponent.ctx.deactivate is not a function的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
vue+js點(diǎn)擊箭頭實(shí)現(xiàn)圖片切換
這篇文章主要為大家詳細(xì)介紹了vue+js點(diǎn)擊箭頭實(shí)現(xiàn)圖片切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06
Element-Plus?el-col、el-row快速布局及使用方法
這篇文章主要介紹了Element-Plus?el-col、el-row快速布局及使用方法,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-12-12
詳解vue中父子組件傳遞參數(shù)props的實(shí)現(xiàn)方式
這篇文章主要給大家介紹了在vue中,父子組件傳遞參數(shù)?props?實(shí)現(xiàn)方式,文章通過代碼示例介紹的非常詳細(xì),對我們的學(xué)習(xí)或工作有一定的參考價值,需要的朋友可以參考下2023-07-07
vue中使用vue-cli接入融云實(shí)現(xiàn)即時通信
這篇文章主要介紹了vue中使用vue-cli接入融云實(shí)現(xiàn)即時通信的相關(guān)資料,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04

