项目练习: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);
          });
        }
      });
    }
  }
}
相关推荐
excel2 小时前
ES6 中函数的双重调用方式:fn() 与 fn\...``
前端
可乐爱宅着2 小时前
全栈框架next.js入手指南
前端·next.js
你的人类朋友3 小时前
什么是API签名?
前端·后端·安全
会豪5 小时前
Electron-Vite (一)快速构建桌面应用
前端
中微子6 小时前
React 执行阶段与渲染机制详解(基于 React 18+ 官方文档)
前端
唐某人丶6 小时前
教你如何用 JS 实现 Agent 系统(2)—— 开发 ReAct 版本的“深度搜索”
前端·人工智能·aigc
中微子6 小时前
深入剖析 useState产生的 setState的完整执行流程
前端
遂心_6 小时前
JavaScript 函数参数传递机制:一道经典面试题解析
前端·javascript
Gracemark6 小时前
高德地图-地图选择经纬度问题【使用输入提示-使用Autocomplete进行联想输入】(复盘)
vue.js
小徐_23336 小时前
uni-app vue3 也能使用 Echarts?Wot Starter 是这样做的!
前端·uni-app·echarts