Vue vee-validate插件如何安装和导入,用法是怎样
Admin 2022-05-23 群英技术资讯 586 次浏览
npm i vee-validate@4.0.3
import { Form, Field } from 'vee-validate'
// 创建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个数字' return true }, isAgree (value) { if (!value) return '请勾选同意用户协议' 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中哪个校验规则 //3. `Field`添加v-model,作用是提供表单数据的双向绑定 //4. 发生表单校验错误,显示错误类名`error`,提示红色边框 <Field v-model="form.account" name="account" type="text" placeholder="请输入用户名" :class="{ error: errors.account }" // 如果返回错误信息,为true 显示类error /> <!-- <input type="text" placeholder="请输入用户名" /> -->
// 表单绑定的数据 const form = reactive({ account: null, // 账号 password: null, // 密码 isAgree: true // 是否选中 }) // 声明当前表单需要的校验数据规则 const curSchema = reactive({ account: schema.account, // 账号 password: schema.password, // 密码 isAgree: schema.isAgree // 是否选中 })
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
这篇文章我们来了解JavaScript迭代器相关内容,下文将介绍什么是迭代器,以及自定义迭代器的方法,文中示例代码介绍的非常详细,对大家学习和理解JavaScript迭代器会有一定的帮助,那么感兴趣的朋友接下来一起跟随小编了解看看吧。
这篇文章主要给大家分享的是vue中常见的开发技巧,小编觉得比较实用,对大家学习或者工作都有一定的帮助,有需要的朋友大家可以借鉴参考,接下来我们一起了解看看吧。
本文主要介绍了JavaScript本地存储实现用户名存储案例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
这篇文章给大家分享的是jQuery如何对事件绑定和解绑的相关内容。小编觉得挺实用的,因此分享给大家做个参考,文中的示例代码介绍得很详细,有需要的朋友可以参考,接下来就跟随小编一起了解看看吧。
前提电脑中已经安装过NodeJS, npm。现在需要进行升级操作。1、查看当前的npm和NodeJs的版本:C:\Users\Administrator>node-vv4.4.3C:\Users\Administrator>npm-version2.15.1 2、升级npm:C:\Users\Administrator>npminstallnpm-gC:\Pr
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008