项目练习: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);
          });
        }
      });
    }
  }
}
相关推荐
aiprtem11 分钟前
基于Flutter的web登录设计
前端·flutter
浪裡遊14 分钟前
React Hooks全面解析:从基础到高级的实用指南
开发语言·前端·javascript·react.js·node.js·ecmascript·php
why技术22 分钟前
Stack Overflow,轰然倒下!
前端·人工智能·后端
幽络源小助理26 分钟前
SpringBoot基于Mysql的商业辅助决策系统设计与实现
java·vue.js·spring boot·后端·mysql·spring
GISer_Jing26 分钟前
0704-0706上海,又聚上了
前端·新浪微博
止观止1 小时前
深入探索 pnpm:高效磁盘利用与灵活的包管理解决方案
前端·pnpm·前端工程化·包管理器
whale fall1 小时前
npm install安装的node_modules是什么
前端·npm·node.js
烛阴1 小时前
简单入门Python装饰器
前端·python
袁煦丞2 小时前
数据库设计神器DrawDB:cpolar内网穿透实验室第595个成功挑战
前端·程序员·远程工作
天天扭码2 小时前
从图片到语音:我是如何用两大模型API打造沉浸式英语学习工具的
前端·人工智能·github