Vue从零开始开发管理系统(学习笔记第二节)

前言

本节笔记记录了如何完善这个管理系统的登录页面。

主要实现为表单添加登录前的校验规则。


一、为表单添加校验规则

  1. 首先在el-form中添加:rules='LoginFormRules'的参数。
  2. 在data数据中心中增加LoginFormRules:{}。
  3. 在LoginFormRules中编写具体的规则
                LoginFormRules:{
                    username:[
                        {required:true,message:'请输入用户名',trigger:'blur'},
                        {min:3,max:10,message:'长度在3和10个字符',trigger:'blur'}
                    ],
                    password:[
                        {required:true,message:'请输入密码',trigger:'blur'},
                        {min:3,max:10,message:'长度在3和10个字符之间',trigger:'blur'}
                    ]
                }

    写法为:

    变量名:[

      {required:是否为必须,message:'提示信息',trigger:'当什么时候出现'},

      {min:最小长度,max:最大长度,message:'提示信息',trigger:'当什么时候出现'}

    ]

    其中blur为鼠标失去焦点时。

  4.  在el-form-item中添加prop传值。
  5. 注意如果出现无法校验的情况,请检查数据中心中的字段名和prop传值是否一致。

二、为表单添加重置功能

  1. 在el-form中添加ref标识,ref='LoginFormRef'
  2. 在methods中编写方法:
    restcheck(){
        this.$refs.LoginFormRef.restFields()
    }

  3. 将restcheck方法绑定至重置button上。 

三、将数据中心重写

  1. 在data数据中心中将username和password写在同一个数组中。
                form:{
                    username:'',
                    password:''
                }

  2. 在el-form中添加:model='form',再在el-form-item中添加v-model为form.username和form.password的格式。
                   <el-form :rules="LoginFormRules" :model="form" ref="LoginFormRef">
                        <el-form-item prop="username">
                            <el-input clearable v-model="form.username"></el-input>
                        </el-form-item>
                        <el-form-item prop="password">
                            <el-input show-password v-model="form.password"></el-input>
                        </el-form-item>
                        <el-form-item style="display:flex; justify-content: flex-end">
                            <el-button type="primary">登录</el-button>
                            <el-button type="info" @click="restcheck()">重置</el-button>
                        </el-form-item>
                    </el-form>
     

总结

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bigdata大星星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值