- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > web開(kāi)發(fā) > JavaScript >
- Vue vee-validate插件的簡(jiǎn)單使用
npm i vee-validate@4.0.3
import { Form, Field } from 'vee-validate'
// 創(chuàng )建js文件進(jìn)行導出 export default { // 校驗項account account (value) { if (!value) return '不能為空'// 條件判斷, return true // 最后全部通過(guò)必須return true }, password (value) { if (!value) return '請輸入密碼' if (!/^\w{6,24}$/.test(value)) return '密碼是6-24個(gè)字符' 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個(gè)數字' return true }, isAgree (value) { if (!value) return '請勾選同意用戶(hù)協(xié)議' return true } }
// validation-schema="mySchema" 配置校驗規則 // v-slot:導出錯誤對象 <Form :validation-schema="mySchema" v-slot="{ errors }" > <!-- 表單元素 --> </Form> <script> import schema from '@/utils/vee-validate-schema' setup () { // 表單對象數據 const form = reactive({ account: null, // 賬號 password: null // 密碼 }) // 校驗規則對象 const mySchema = { account: schema.account, password: schema.password } return { form, mySchema } } </script>
//1. 把input改成 `Field` 組件,默認解析成input //2. `Field` 添加name屬性,作用是指定使用schema中哪個(gè)校驗規則 //3. `Field`添加v-model,作用是提供表單數據的雙向綁定 //4. 發(fā)生表單校驗錯誤,顯示錯誤類(lèi)名`error`,提示紅色邊框 <Field v-model="form.account" name="account" type="text" placeholder="請輸入用戶(hù)名" :class="{ error: errors.account }" // 如果返回錯誤信息,為true 顯示類(lèi)error /> <!-- <input type="text" placeholder="請輸入用戶(hù)名" /> -->
// 表單綁定的數據 const form = reactive({ account: null, // 賬號 password: null, // 密碼 isAgree: true // 是否選中 }) // 聲明當前表單需要的校驗數據規則 const curSchema = reactive({ account: schema.account, // 賬號 password: schema.password, // 密碼 isAgree: schema.isAgree // 是否選中 })
以上就是Vue vee-validate插件的簡(jiǎn)單使用的詳細內容,更多關(guān)于Vue vee-validate插件的資料請關(guān)注腳本之家其它相關(guān)文章!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站