問題回顧:
1、vue項目的在彈窗上的form表單驗證,第一次點擊新增時正常,第二次新增打開彈窗后由於表單內容為空,出現驗證這種情況
2、為了解決上面的情況,在執行點擊新增事件加上this.$refs[formName].resetFields()或者this.$refs[formName].clearValidate();
3、刷新界面后第一次點擊新增出現這樣"Cannot read property 'resetFields' of undefined"或者"Cannot read property 'clearValidate' of undefined",
第二次點擊新增沒有報錯沒有新增,說明第二次沒有報錯。
問題分析:刷新界面后第一次新增,此時表單內的dom還沒有加載完成就執行了"Cannot read property 'resetFields' of undefined"或者"Cannot read property 'clearValidate' of undefined",
這樣就導致報錯。
修改意見
//打開彈窗的新增函數 addStaff() { this.staffVisible = true;//彈窗打開 this.$nextTick(()=>{ this.$refs.staffForm.resetFields();//等彈窗里的form表單的dom渲染完在執行this.$refs.staffForm.resetFields(),去除驗證 }); },