不要改变形参的值
onServiceCardAdd({
currentTarget: {
dataset: {
item,
}
}
}) {
const initPrepurchases = this.data.pagePrepurchases ? this.data.pagePrepurchases : this.data.initPrepurchases;
if (item.limitNumber > 0 && item.count <= item.maxCount) {
item.limitNumber -= 1;
item.count += 1;
this.data.num++;
initPrepurchases.map(initItem => {
if (item.id === initItem.id) {
// initItem的值发生了变化,数据源如果被用在了其他地方就有可能导致逻辑错误
initItem.count = item.count;
}
});
this.setData({
num: this.data.num
});
const storeId = this.data.storeInfo.id;
this.requestBookingPrepurchaseMaxCount({storeId, prePurechases: initPrepurchases});
}
}
// 不建议
this.data.skuList.forEach(item => {item['isSelected'] = true; });
// 建议
this.data.skuList = this.data.skuList.map(item => ({
...item,
isSelected: true,
}));