- 资讯首页 > 开发技术 > web开发 > JavaScript >
- Vue vee-validate插件的简单使用
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 // 是否选中
})
以上就是Vue vee-validate插件的简单使用的详细内容,更多关于Vue vee-validate插件的资料请关注脚本之家其它相关文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、来自本网站内容采集于网络互联网转载等其它媒体和分享为主,内容观点不代表本网站立场,如侵犯了原作者的版权,请告知一经查实,将立刻删除涉嫌侵权内容,联系我们QQ:712375056,同时欢迎投稿传递力量。
Copyright © 2009-2022 56dr.com. All Rights Reserved. 特网科技 特网云 版权所有 特网科技 粤ICP备16109289号
域名注册服务机构:阿里云计算有限公司(万网) 域名服务机构:烟台帝思普网络科技有限公司(DNSPod) CDN服务:阿里云计算有限公司 百度云 中国互联网举报中心 增值电信业务经营许可证B2
建议您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流浏览器浏览本网站


