解决:Cannot read properties of undefined (reading ‘validate‘)问题

问题:Element UI使用表单校验功能控制台出现Cannot read properties of undefined (reading 'validate')报错

解决:在 <el-form :model="form" :rules="rules">添加 ref="form",form为自定义的表单名称

html 复制代码
 <el-form :model="form" :rules="rules" ref="form"></l-form>

完整案例

添加/修改表单代码

html 复制代码
<!-- 新增/更新 -->
    <div>
      <el-dialog :title="(form.id == null ? '新增' : '更新')" :visible.sync="dialogFormVisible" width="40%">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="学号" prop="number" label-width="20%">
          <el-input v-model="form.number" autocomplete="off" placeholder="请输入学号" style="width:80%"/>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('form')">取 消</el-button>
        <el-button type="primary" @click="submit('form')">确 定</el-button>
      </div>
    </el-dialog>
    </div>

js

javascript 复制代码
<script>
    export default {
      data() {
        return {
            // 表单
            form:{},
            // 表单模态框
            dialogFormVisible:false,
            // 新增/更新表单校验
            rules: {
                 number: [
                    { required: true, message: '学号不能为空', trigger: 'blur' }
                 ],
             },
    	}
    }

    methods:{
    	// 提交表单数据
        async submit(form){
          this.$refs[form].validate(async (valid) => {
            if (valid) {
              await this.apiInsert()
              this.select()
            }
          })
        },
        apiInsert(){
          return new Promise(resolve => {
            insert(this.form).then(res => {
              if(res.code === 200){
                this.dialogFormVisible = false
                resolve()
              }else{
                this.$message.error(res.message.message)
                resolve()
              }
            })
          })
        },
        // 取消
        resetForm(form) {
          this.dialogFormVisible = false
          this.$refs[form].resetFields();
        },
    }
  }
</script>
相关推荐
Cyber4K8 小时前
【Python专项】进阶语法-系统资源监控与数据采集(1)
开发语言·python·php
Le_ee8 小时前
ctfweb:php/php短标签/.haccess+图片马/XXE
开发语言·前端·php
爱上好庆祝8 小时前
学习js的第七天(wed APIs的开始)
前端·javascript·css·学习·html·css3
yong99909 小时前
MATLAB读取高光谱图像
开发语言·matlab
2zcode9 小时前
基于MATLAB的肝病风险评估与分期分析系统设计与实现
开发语言·matlab
小小de风呀9 小时前
de风——【从零开始学C++】(五):内存管理
开发语言·c++
ooseabiscuit9 小时前
Laravel6.x核心优化与特性全解析
android·开发语言·javascript
折哥的程序人生 · 物流技术专研9 小时前
Java面试85题图解版(一):基础核心篇
java·开发语言·后端·面试
哆啦A梦158810 小时前
20, Springboot3+vue3实现前台轮播图和详情页的设计
javascript·数据库·spring boot·mybatis·vue3
Hello.Reader10 小时前
算法基础(十)——分治思想把大问题拆成小问题
java·开发语言·算法