项目练习: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);
          });
        }
      });
    }
  }
}
相关推荐
ayqy贾杰30 分钟前
Agent First Engineering
前端·vue.js·面试
IT_陈寒40 分钟前
SpringBoot实战:5个让你的API性能翻倍的隐藏技巧
前端·人工智能·后端
iceiceiceice1 小时前
iOS PDF阅读器段评实现:如何从 PDFSelection 精准还原一个自然段
前端·人工智能·ios
大金乄1 小时前
封装一个vue2的elementUI 表格组件(包含表格编辑以及多级表头)
前端·javascript
葡萄城技术团队2 小时前
【性能优化篇】面对万行数据也不卡顿?揭秘协同服务器的“片段机制 (Fragments)”
前端
程序员阿峰2 小时前
2026前端必备:TensorFlow.js,浏览器里的AI引擎,不写Python也能玩转智能
前端
Jans2 小时前
Shipfe — Rust 写的前端静态部署工具:一条命令上线 + 零停机 + 可回滚 + 自动清理
前端
徐小夕2 小时前
JitWord 2.3: 墨定,行远
前端·vue.js·github
南果梨3 小时前
OpenClaw 完整教程!从安装到使用(官方脚本版)
前端·git·开源
大雨还洅下3 小时前
前端手写: new操作符
前端