解决: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>
相关推荐
cnxy1882 小时前
围棋对弈Python程序开发完整指南:步骤1 - 棋盘基础框架搭建
开发语言·python
wuhen_n2 小时前
LeetCode -- 15. 三数之和(中等)
前端·javascript·算法·leetcode
脾气有点小暴3 小时前
scroll-view分页加载
前端·javascript·uni-app
程序员-周李斌3 小时前
Java 死锁
java·开发语言·后端
JasmineWr4 小时前
CompletableFuture相关问题
java·开发语言
零雲4 小时前
java面试:知道java的反射机制吗
java·开发语言·面试
Jeremy爱编码4 小时前
实现 Trie (前缀树)
开发语言·c#
前端开发爱好者4 小时前
VSCode 重磅更新!要收费了?
前端·javascript·visual studio code
laocooon5238578864 小时前
插入法排序 python
开发语言·python·算法
你的冰西瓜5 小时前
C++中的list容器详解
开发语言·c++·stl·list