需求中要写一个修改表单,一个添加表单,为避免代码重复,这里两个表单共用了一套代码。并在表单关闭事件中写了重置表单事件:
// 关闭编辑对话框时执行
handleDialogClose() {
this.$refs.editForm.resetFields();
this.addUserdialogFormVisible = false;
},
1.问题:先打开修改表单,然后打开添加表单,添加表单总是会带出
第一次打开的修改表单的内容
修改表单:
添加表单:
2.resetFields()不生效的原因
- 1.resetFields()方法用于将form表单的数据设置为初始值
- 2.但这个初始值是在form mounted生命周期被赋值上去的
- 3.所以,在 form mounted之前,如果给form表单赋值了,那么后面调用resetFields()都是无效的,因为form表单的初始值已经在 mounted 之前就被赋值了
3.解决方法:
只要在 form 表单 mounted之后再进行赋值操作这样就可以完美解决问题了,即在点击编辑赋值的时候使用 “this.$nextTick” 方法。
// 点击修改按钮
async openEditDialog(row) {
this.dialogStatus = "update";
this.addDelete = "修改用户";
this.addUserdialogFormVisible = true;
this.disabled = true;
this.disabledAudit = false;
this.$nextTick(() => {
this.id = row.id;
this.form.unitType = row.unitType;
this.form.unitName = row.unitName;
this.form.unitCode = row.unitCode;
this.form.loginUsername = row.loginUsername;
this.form.managerName = row.managerName;
this.form.phone = row.phone;
this.form.email = row.email;
this.form.enabled = row.enabled;
this.form.status = row.status;
});
},
this.$refs.editForm.resetFields();重置表单生效