for循环let的问题
来源:11-4 使用 computed 完成订单价格计算

慕粉1473424859
2023-08-08
const total = computed(() => {
const productList = cartList[shopId]
let count = 0
if (productList) {
for (var i in productList) {
const product = productList[i]
count += product.count
}
}
return count
})
return { total }
for循环里面用let 的话报错:error ‘i’ is never reassigned. Use ‘const’ instead 是怎么回事啊,老师
写回答
2回答
-
Dell
2023-08-16
i 在循环中并没有地方修改它,如果不修改这个i,那这个i 要求你用 const 定义
00 -
imamyke
2023-08-12
因为 productList 里面的 key 不会重复相同的值,所以 i 这个变量也不会存在重新赋值这件事,严谨来说用const 会更符合 Standard ESLint 规范,才不会报错。
00
相似问题