久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

Vue vee-validate插件的簡單使用

瀏覽:120日期:2022-09-28 17:05:28
目錄1.安裝2.導入3.定義校驗規(guī)則(最好是在utils文件夾中單獨封裝js文件導出)4.使用Form組件配置校驗規(guī)則和錯誤對象 (form 和 Field都是從插件中按需導出)5.使用 Field 組件,添加表單項目校驗6.補充表單數(shù)據(jù)和驗證規(guī)則數(shù)據(jù)1.安裝

npm i vee-validate@4.0.32.導入

import { Form, Field } from ’vee-validate’3.定義校驗規(guī)則(最好是在utils文件夾中單獨封裝js文件導出)

// 創(chuàng)建js文件進行導出export default { // 校驗項account account (value) { if (!value) return ’不能為空’// 條件判斷, return true // 最后全部通過必須return true }, password (value) { if (!value) return ’請輸入密碼’ if (!/^w{6,24}$/.test(value)) return ’密碼是6-24個字符’ return true }, mobile (value) { if (!value) return ’請輸入手機號’ if (!/^1[3-9]d{9}$/.test(value)) return ’手機號格式錯誤’ return true }, code (value) { if (!value) return ’請輸入驗證碼’ if (!/^d{6}$/.test(value)) return ’驗證碼是6個數(shù)字’ return true }, isAgree (value) { if (!value) return ’請勾選同意用戶協(xié)議’ return true }}4.使用Form組件配置校驗規(guī)則和錯誤對象 (form 和 Field都是從插件中按需導出)

// validation-schema='mySchema' 配置校驗規(guī)則// v-slot:導出錯誤對象<Form :validation-schema='mySchema' v-slot='{ errors }'> <!-- 表單元素 --></Form><script> import schema from ’@/utils/vee-validate-schema’ setup () { // 表單對象數(shù)據(jù) const form = reactive({ account: null, // 賬號 password: null // 密碼 }) // 校驗規(guī)則對象 const mySchema = { account: schema.account, password: schema.password } return { form, mySchema } } </script>5.使用 Field 組件,添加表單項目校驗

//1. 把input改成 `Field` 組件,默認解析成input//2. `Field` 添加name屬性,作用是指定使用schema中哪個校驗規(guī)則//3. `Field`添加v-model,作用是提供表單數(shù)據(jù)的雙向綁定//4. 發(fā)生表單校驗錯誤,顯示錯誤類名`error`,提示紅色邊框<Field v-model='form.account' name='account' type='text' placeholder='請輸入用戶名' : // 如果返回錯誤信息,為true 顯示類error /> <!-- <input type='text' placeholder='請輸入用戶名' /> -->6.補充表單數(shù)據(jù)和驗證規(guī)則數(shù)據(jù)

// 表單綁定的數(shù)據(jù)const form = reactive({ account: null, // 賬號 password: null, // 密碼 isAgree: true // 是否選中})// 聲明當前表單需要的校驗數(shù)據(jù)規(guī)則const curSchema = reactive({ account: schema.account, // 賬號 password: schema.password, // 密碼 isAgree: schema.isAgree // 是否選中})

以上就是Vue vee-validate插件的簡單使用的詳細內(nèi)容,更多關(guān)于Vue vee-validate插件的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 榆树市| 同仁县| 从化市| 双峰县| 聊城市| 中方县| 云霄县| 布尔津县| 彭山县| 县级市| 凌云县| 灌云县| 陆丰市| 鹿泉市| 沂源县| 密云县| 宁都县| 肃宁县| 泗阳县| 武穴市| 罗江县| 江城| 额济纳旗| 得荣县| 珠海市| 徐闻县| 扶绥县| 凤冈县| 文登市| 平邑县| 永泰县| 车险| 海淀区| 砚山县| 涟源市| 通榆县| 蒙自县| 通河县| 渝中区| 三明市| 儋州市|