为什么不把购物车的count为的进行删除,二是count为0的仅仅不显示?

来源:12-10 购物流程开发完成(2)

qq_Simpleisbeau_0

2022-03-07

store/index.js的商品修改changeCartItemInfo 函数

changeCartItemInfo (state, payload) {
      const { shopId, productId, productInfo, num } = payload
      const shopInfo = state?.cartList?.[shopId] || {
        shopName: '', productList: {}
      }
      let product = shopInfo.productList[productId]
      if (!product) {
        productInfo.count = 0
        product = productInfo
      }
      product.count += num
      if (num > 0) product.isCheck = true
      if (product.count <= 0) {
        delete shopInfo.productList[productId]
      } else {
        shopInfo.productList[productId] = product
      }
      state.cartList[shopId] = shopInfo
      setLocalCartList(state)
    }

这样写可否,我直接把count为0,用“delete shopInfo.productList[productId]”给删了。实际开发也是对这些数据软删除吗,有什么深层含义吗,请老师告知。

写回答

1回答

Dell

2022-03-12

不建议这么操作,因为你用的vue proxy 生成的对象,这么操作有可能会有意想不到的额外 bug

0
1
qq_Simpleisbeau_0
好的,谢谢
2022-03-17
共1条回复

Vue3入门与项目实战 掌握完整知识体系

明星讲师DELL亲授,全方位知识点+高匹配度项目,入门到深度掌握

3382 学习 · 1454 问题

查看课程