不刷新页面重新打开el-dialog时,如果我们绑定了rules或者某个值需要required,它总会自动校验。查看了多个博文,发现常用的有两种解决方法(下列方法都可以在其他博文查看,不再细写,如有需要请自行查询):1.给dialog套上v-if ;2.在关闭dialog时,监听关闭回调,清除校验。我在自己的项目里使用了上述两种方法,都不太好用,自己琢磨出了另一种方法:


调用dialog打开的回调
methods:

openDialog(){

this.$nextTick(() => {

this.$refs.dataForm.clearValidate();

})
}
methods:

openDialog(){

this.$nextTick(() => {

this.$refs.dataForm.clearValidate();

})
}初始化dialog时, 拿到变化后的dom, 进行清除校验补充知识:Cannot read property 'resetFields' of undefined 问题及引申补充知识:补充知识:Cannot read property 'resetFields' of undefined 问题及引申问题描述: 使用element开发我的后台系统,编辑和新增使用了同一个弹出框问题描述: 绑定了数据data里的commentForm对象为了在新增弹出框清空表单, 使用了this.$refs[formName].resetFields()每次第一次点击新增显示弹出框,都会报错
"[Vue warn]: Error in event handler for "click": "TypeError: Cannot read property 'resetFields' of undefined""
"[Vue warn]: Error in event handler for "click": "TypeError: Cannot read property 'resetFields' of undefined""问题原因: 问题原因: mouted加载table数据以后,隐藏的弹出框并没有编译渲染进dom里面。所以@click="dialogFormVisible = true;resetForm('dlgForm')"click弹出的时候$refs并没有获取到dom元素导致 'resetFields' of undefined解决方法:解决方法:1、($nextTick dom下一次更新之后)

resetForm(formName) {

this.$nextTick(()=>{

this.$refs[formName].resetFields();

})


},

resetForm(formName) {

this.$nextTick(()=>{

this.$refs[formName].resetFields();

})


},2、(如果是第一次就点击新增就没必要reset, 根据元素undefined判断)

if (this.$refs[formName] !== undefined) {

this.$refs[formName].resetFields();

}

if (this.$refs[formName] !== undefined) {

this.$refs[formName].resetFields();

}注意事项:对DOM一系列的js操作最好都要放进Vue.nextTick()的回调函数中以上这篇基于element-ui对话框el-dialog初始化的校验问题解决就是小编分享给大家的全部内容了,希望能给大家一个参考。