敢为人鲜小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

22 lines
673 B

  1. // store/modules/cartNum.js
  2. // 这是管理分类列表加入购物车数量的store
  3. import Vue from 'vue'
  4. export default {
  5. namespaced: true,
  6. state: {
  7. productCounts: {} // 格式:{ 商品ID: 数量 }
  8. },
  9. mutations: {
  10. // 增加商品数量
  11. incrementProduct(state, { productId, amount = 1 }) {
  12. const currentCount = state.productCounts[productId] || 0
  13. Vue.set(state.productCounts, productId, currentCount + amount)
  14. }
  15. },
  16. getters: {
  17. // 获取指定商品数量
  18. getProductCount: (state) => (productId) => {
  19. return state.productCounts[productId] || 0
  20. }
  21. }
  22. }