记录工作中一个bug
场景
表单中选择业务类型后,调取不同业务的接口获取数据。
坑
let obj = {
trainorg:trainorgList({ current: this.current }),
org: orgList({ current: this.current }),
projectorg: projectorgList({ current: this.current }),
evalorg: evalorgList({ current: this.current }),
};
obj[type].then((res) => {
this.orgOptions = res.data.data.records.map((item) => {
return {
name: item.name,
orgId: item[type + 'Id'],
};
});
this.total = res.data.data.total;
});
访问对象中的某一个属性时,对象中所有的方法都执行了一遍。
解决办法
将对象中的方法套一层函数return出主体函数,以此防止函数自执行。
let obj = {
trainorg: function () {
return trainorgList({ current: this.current });
},
org: function () {
return orgList({ current: this.current });
},
projectorg: function () {
return projectorgList({ current: this.current });
},
evalorg: function () {
return evalorgList({ current: this.current });
},
};
obj[type]().then((res) => {
this.orgOptions = res.data.data.records.map((item) => {
return {
name: item.name,
orgId: item[type + 'Id'],
};
});
this.total = res.data.data.total;
});
至此,问题解决