项目中用到的,多选添加,在for循环里面实现的。
你选择了多个数据,提示语句也会有多个,但是每条的提示语句名字都一样
如下代码
for (let i = 0; i < this.nowData.length; i++) {
a = this.nowData[i].functionId;
name = this.nowData[i].name
BaseTableSub({
functionId: a,
productId: this.KFproID
})
.then(res => {
console.log(res);
if (res.status == 200) {
this.$message.success(`${name} +:添加成功`);
this.ADDbase_dialogFormVisible = false;
this.nowData = [];
this.InitBaseTable();
}
})
.catch(err => {
console.log(err.response);
return this.$message.error(name +':'+ err.response.data.message);
});
}
原因就是:for循环里提示只执行最后一次提示
修改为这样的代码:
使用自执行闭包函数,能把我们用到的参数存起来。
for (let i = 0; i < this.nowData.length; i++) {
a = this.nowData[i].functionId;
name = this.nowData[i].name
;((a,name) => {
console.log(111);
console.log(a,name);
BaseTableSub({
functionId: a,
productId: this.KFproID
}).then(res => {
console.log(res);
if (res.status == 200) {
this.$message.success(`${name} :添加成功`);
this.ADDbase_dialogFormVisible = false;
this.nowData = [];
this.InitBaseTable();
}
}).catch(err => {
console.log(err.response);
return this.$message.error(name + ':' + err.response.data.message);
});
})(a,name)
}