// store/modules/cartNum.js // 这是管理分类列表加入购物车数量的store import Vue from 'vue' export default { namespaced: true, state: { productCounts: {} // 格式:{ 商品ID: 数量 } }, mutations: { // 增加商品数量 incrementProduct(state, { productId, amount = 1 }) { const currentCount = state.productCounts[productId] || 0 Vue.set(state.productCounts, productId, currentCount + amount) } }, getters: { // 获取指定商品数量 getProductCount: (state) => (productId) => { return state.productCounts[productId] || 0 } } }