项目练习:element-ui的valid表单验证功能用法

文章目录

一、情景说明

一般表单提交的时候,都要对表单数据进行前段验证。

比如登陆表单提交。

二、代码实现

package.json

json 复制代码
    "element-ui": "2.15.14",

main.js 引用ElementUI

bash 复制代码
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

Vue.config.productionTip = false
Vue.use(ElementUI);

Vue组件中的html 代码

代码说明:el-form表单中,配置:rules="loginRules"和ref="loginForm"属性

html 复制代码
    <el-form ref="loginForm" class="login-form" :model="loginForm" :rules="loginRules">
        <el-form-item prop="username">
        </el-form-item>
    </el-form>

Vue组件中的js代码

代码说明:
loginRules规则
this.$refs.loginForm.validate(valid => {})

js 复制代码
  export default {
  name: 'login',
  data(){
    return{
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      }
    }
  },
  methods:{
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          const username = this.loginForm.username.trim()
          const password = this.loginForm.password
          const code = this.loginForm.code
          const uuid = this.loginForm.uuid
          login(username, password, code, uuid).then(res => {
            console.log('1111',res);
          });
        }
      });
    }
  }
}
相关推荐
C_心欲无痕25 分钟前
nginx - alias 和 root 的区别详解
运维·前端·nginx
北辰alk38 分钟前
Vue 路由信息获取全攻略:8 种方法深度解析
vue.js
北辰alk42 分钟前
Vue 三剑客:组件、插件、插槽的深度辨析
vue.js
北辰alk1 小时前
Vue Watch 立即执行:5 种初始化调用方案全解析
vue.js
北辰alk1 小时前
Vue 组件模板的 7 种定义方式:从基础到高级的完整指南
vue.js
北辰alk1 小时前
深入理解 Vue 生命周期:created 与 mounted 的核心差异与实战指南
vue.js
计算机毕设VX:Fegn08951 小时前
计算机毕业设计|基于springboot + vue小型房屋租赁系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
北辰alk1 小时前
Vuex日渐式微?状态管理的三大痛点与新时代方案
vue.js
我是苏苏3 小时前
Web开发:C#通过ProcessStartInfo动态调用执行Python脚本
java·服务器·前端