Uncaught TypeError: Cannot read property 'resetFields' of undefined

参考链接:
https://blog.csdn.net/bobobocai/article/details/91437656

  • 在用vue+elementUI开发后台系统的时候,登录和注册用了同一个弹出框。为了在新增弹出框清空表单, 使用了 this.$refs[ruleForm].resetFields()
    报错信息:
    image.png

    报错代码:
    image.png

    问题原因:
    mounted加载table数据后,隐藏的弹出框冰没有编译到DOM里面。所以每次点击出现Dialog的时候,$refs冰没有获取到DOM元素导致'resetFields' of undefined

解决方案:
1.判断是不是第一次点击,如果是第一次就点击就没必要清空 Dialog 表单数据, 根据this.nextTick,保证DOM已经渲染成功

showModal() {
      this.$nextTick(() => {
        if (this.$refs['ruleForm'] !== undefined) {
          this.$refs['ruleForm'].resetFields()
        }
      })
    },

你可能感兴趣的:(Uncaught TypeError: Cannot read property 'resetFields' of undefined)