项目练习: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_心欲无痕4 小时前
前端页面渲染方式:CSR、SSR、SSG
前端
果粒蹬i4 小时前
生成式 AI 质量控制:幻觉抑制与 RLHF 对齐技术详解
前端·人工智能·easyui
招风的黑耳4 小时前
Axure后台管理系统模板:智慧化解决方案的界面设计蓝图
ui·axure·后台原型
WordPress学习笔记5 小时前
解决Bootstrap下拉菜单一级链接无法点击的问题
前端·bootstrap·html
Never_Satisfied5 小时前
C#插值字符串中大括号表示方法
前端·c#
踢球的打工仔6 小时前
typescript-类
前端·javascript·typescript
天天打码6 小时前
Svelte-无虚拟DOM、极致性能的现代高性能Web开发框架!
前端·node.js·vue·svelte
0思必得06 小时前
[Web自动化] Selenium元素定位
前端·python·selenium·自动化·html
weixin_419658316 小时前
UISpy:Windows 界面控件的“显微镜“[特殊字符]
windows·python·测试工具·ui
EEEzhenliang6 小时前
CSS知识概括、总结
前端·css