提供的方法依旧不能解决问题
时间: 2024-03-25 08:37:23 浏览: 20
如果使用了`splice`方法,确实会导致后面的商品的索引发生变化,因此删除商品时应该先将该商品的引用从数组中移除,然后再将修改后的数组重新赋值给`shoppingCar`。可以尝试以下修改:
```
if (this.shoppingCar[i].numValue == 0) {
this.shoppingCar.splice(i, 1);
break;
}
```
完整代码如下:
```
handleChange(value) {
var numDom = this.$refs;
for (var i = 0; i <= this.shoppingCar.length - 1; i++) {
if (value.itemId == this.shoppingCar[i].itemId) {
if (this.sum !== 0) {
this.sum=(this.sum.toFixed(2)*100000000000000)/100000000000000;
}
this.shoppingCar[i].numValue = parseInt(numDom[value.itemId][0].inputValue)
//如果购物车中商品数量为0
if (this.shoppingCar[i].numValue == 0) {
this.shoppingCar.splice(i, 1);
break;
}
this.shoppingItem.data = JSON.parse(uni.getStorageSync(this.sellerId))
break;
}
}
this.shoppingItem.data.forEach(item => {
item.numValue = numDom[item.itemId][0].inputValue
})
this.sumAdd()
this.sumCount()
uni.setStorageSync(this.sellerId, JSON.stringify(this.shoppingCar))
uni.setStorageSync('doAccounts', JSON.stringify(this.sum))
uni.setStorageSync('count', JSON.stringify(this.count))
this.shoppingItem.data = JSON.parse(uni.getStorageSync(this.sellerId))
}
```
这样应该可以避免删除商品时对其他商品产生影响了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)