vue實(shí)現(xiàn)表單單獨(dú)移除一個(gè)字段驗(yàn)證
本文實(shí)例為大家分享了vue實(shí)現(xiàn)表單單獨(dú)移除一個(gè)字段驗(yàn)證的具體代碼,供大家參考,具體內(nèi)容如下
下面的代碼以登錄功能為例。
功能描述: 用戶輸入密碼時(shí),失去焦點(diǎn)進(jìn)行密碼校驗(yàn),當(dāng)輸入有誤時(shí),顯示錯(cuò)誤提示信息;只要密碼值發(fā)生了變化,錯(cuò)誤提示信息就移除。
通過watch監(jiān)控password字段,實(shí)現(xiàn)實(shí)時(shí)移除。
vue2 模板語法代碼如下:
<template> ? ? <div> ? ? ? ? ?<el-form :model="form" :rules="rules" ref="form" label-width="140px"> ? ? ? ? ? <el-form-item label="用戶名" prop="username"> ? ? ? ? ? ?<el-input v-model="form.username" placeholder="請(qǐng)輸入用戶名"></el-input> ? ? ? ? ? </el-form-item> ? ? ? ? ? <el-form-item label="密碼" prop="password"> ? ? ? ? ? ? <el-input type="password" v-model="form.password" placeholder="請(qǐng)輸入密碼" ? ? ? ? ? ? ></el-input> ? ? ? ? ? </el-form-item> ? ? ? ? ? <el-form-item> ? ? ? ? ? ? <el-button type="primary" @click="submit('form')">提交</el-button> ? ? ? ? ? ? <el-button @click="reset('form')">取消</el-button> ? ? ? ? ? </el-form-item> ? ? ? ? </el-form> ? ? <div> </template> <script> export default { ? ? data() { ? ? ? ? let validatePwd ?= (rule, value, callback) => { ? ? ? ? ? ? // 密碼校驗(yàn) ? ? ? ? } ? ? ? ? return { ? ? ? ? ? ? form: { ? ? ? ? ? ? ? ? username: '', ? ? ? ? ? ? ? ? password: '' ? ? ? ? ? ? }, ? ? ? ? ? ? ?rules: { ? ? ? ? ? ? ? ? username: [{ required: true, message: '請(qǐng)輸入用戶名', trigger: 'blur'}], ? ? ? ? ? ? ? ? password: [{ required: true, validator: validatePwd, trigger: 'blur'}] ? ? ? ? ? ? } ? ? ? ? } ? ? }, ? ? watch: { ? ? ? ? 'form.password'(newVal, oldVal) { ? ? ? ? ? ? if(newVal, oldVal) { ? ? ? ? ? ? ? ? ? this.$refs['form'].clearValidate('password'); ? ? ? ? ? ? } ? ? ? ? } ? ? } ? } </script>
vue2+composition-api 使用tsx語法開發(fā),代碼如下:
service.ts
export const initFormState = { ? ? username: '', ? ? password: '' } ? const validatePwd = (rule, value, callback) => { ? ? // 密碼校驗(yàn) } ? export const formStateRules = { ? ? username: [ ? ? ? ? { required: true, message: '請(qǐng)輸入用戶名', trigger: 'blur'} ? ? ], ? ? password: [ ? ? ? ? { required: true, validator: validatePwd, trigger: 'blur'} ? ? ] }
index.tsx文件
import { onMounted, reactive, ref, watch } from "@vue/composition-api"; import { initFormState} from './service.ts'; ? export default defineComponent({ ? ? setup (prop,{root}) { ? ? ? ? ? ? ? const formState= reactive({ ? ? ? ? ? ? ...initFormState, // 從service引入進(jìn)來 ? ? ? ? }) ? ? ? ? ? const formRef = ref(null); ? ? ? ?? ? ? ? ? const submit = () ?=> { ? ? ? ? ? ? // 表單校驗(yàn) ? ? ? ? ? ? const formValidate = await new Promise(resolve => { ? ? ? ? ? ? ? ? (formRef as any).value.validate((valid: boolean) => resolve(valid)); ? ? ? ? ? ? }); ? ? ? ? ? ? ? if(!formValidate) return; ? ? ? ? ? ? // 表單校驗(yàn)通過后進(jìn)行的邏輯 ? ? ? ? } ? ? ? ? ? const reset = () => { ? ? ? ? ? ? ?Object.keys(formState).map((key: string) => formState[key] = (initFormState as any)[key]); ? ? ? ? } ? ?? ? ? ? ? // 監(jiān)控password, 按需移除錯(cuò)誤提示信息 ? ? ? ?watch(() => formState.password, ()=> { ? ? ? ? ? ?const result = formState.password; ? ? ? ? ? ? if(result) { ? ? ? ? ? ? ? ? (formRef as any).value.clearValidate('password') ? ? ? ? ? ? } ? ? ? ? }) ? ? ? ? return { ? ? ? ? ? ? formState, ? ? ? ? ? ? formRef, ? ? ? ? ? ? formStateRules, ? ? ? ? ? ? submit, ? ? ? ? ? ? reset ? ? ? ? } ? ?? ? ? }, ? ? render() { ? ? ? ? return ( ? ? ? ? ? ? <div> ? ? ? ? ? ? ? ? <el-form? ? ? ? ? ? ? ? ? ? ? ref="formRef"? ? ? ? ? ? ? ? ? ? ? form={this.formState}? ? ? ? ? ? ? ? ? ? ? props={{model: this.formState}} ? ? ? ? ? ? ? ? ? ? rules={formStateRules} ?label-width="140px"> ? ? ? ? ? ? ? ? ? <el-form-item label="用戶名" prop="username"> ? ? ? ? ? ? ? ? ? ? ? ?<el-input v-model={this.formState.username} placeholder="請(qǐng)輸入用戶名"></el-input> ? ? ? ? ? ? ? ? ? </el-form-item> ? ? ? ? ? ? ? ? ? <el-form-item label="密碼" prop="password"> ? ? ? ? ? ? ? ? ? ? <el-input type="password" v-model={this.formState.password} placeholder="請(qǐng)輸入密碼"></el-input> ? ? ? ? ? ? ? ? ? </el-form-item> ? ? ? ? ? ? ? ? ? <el-form-item> ? ? ? ? ? ? ? ? ? ? <el-button type="primary" onClick={this.submit}>提交</el-button> ? ? ? ? ? ? ? ? ? ? <el-button onClick={this.reset}>取消</el-button> ? ? ? ? ? ? ? ? ? </el-form-item> ? ? ? ? ? ? ? ? </el-form> ? ? ? ? ? ? </div> ? ? ? ? ) ? ? } ? })
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue el-upload單圖片上傳功能實(shí)現(xiàn)
這篇文章主要介紹了Vue el-upload單圖片上傳功能實(shí)現(xiàn),本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-11-11vue引用BootStrap以及引用bootStrap-vue.js問題
這篇文章主要介紹了vue引用BootStrap以及引用bootStrap-vue.js問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10Vue3組合式API中使用forwardRef()函數(shù)
本文主要介紹了Vue3組合式API中使用forwardRef()函數(shù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06