原码如下
dataFormSubmit() {
const formerUnionBranchInfoList = this.dataForm.unionBranchInfoList;
// eslint-disable-next-line max-len
const currentUnionBranchInfoList = formerUnionBranchInfoList.map((item) => ({ branchCode: item, ...this.availableUnionBranches.find((i) => i.branchCode === item) }));
console.log(currentUnionBranchInfoList,'currentUnionBranchInfoList');
this.dataForm.unionBranchInfoList = currentUnionBranchInfoList;
if (!this.dataForm.unionBranchInfoSwitch) {
this.dataForm.unionBranchInfoList = [];
}
this.$refs.dataForm.validate((valid) => {
if (valid) {
this.loading = true;
insertOrUpdate(this.dataForm).then((res) => {
if (res && res.data) {
this.$message.success(`${this.type === 'add' ? '添加' : '更新'}成功`);
this.$emit('refresh');
} else {
this.$message.error(res.msg);
}
}).finally(() => {
this.loading = false;
});
}
});
}
问题出在 dataFormSubmit 方法中对 unionBranchInfoList 的处理上。当前的处理方式会直接修改 dataForm 中的数据,这可能导致视图上的临时变化。
修改后的代码
// ... existing code ...
dataFormSubmit() {
this.$refs.dataForm.validate((valid) => {
if (valid) {
this.loading = true;
// 创建提交数据的副本,避免修改原始数据
const submitData = { ...this.dataForm };
if (submitData.unionBranchInfoSwitch) {
const unionBranchInfoList = submitData.unionBranchInfoList.map((item) => ({
branchCode: item,
...this.availableUnionBranches.find((i) => i.branchCode === item)
}));
submitData.unionBranchInfoList = unionBranchInfoList;
} else {
submitData.unionBranchInfoList = [];
}
insertOrUpdate(submitData).then((res) => {
if (res && res.data) {
this.$message.success(`${this.type === 'add' ? '添加' : '更新'}成功`);
this.$emit('refresh');
} else {
this.$message.error(res.msg);
}
}).finally(() => {
this.loading = false;
});
}
});
},
// ... existing code ...