在mutations中注册的setCart方法:
//store.js mutations:{ setCart(state,foodIndex){ Vue.set(state.goods.foods[foodIndex],"count",1); } }
组件中提交mutations并使用setCart方法
//childComponent.vuemethods:{ setCart(foodIndex){ this.$store.commit('setCart',this.foodIndex); }, }, addCartItem:function (event) { if (!this.food.count){ this.setCart(this.foodIndex); }else{ this.addCart(this.foodIndex) return } },
结果会报错,this.foodIndex is undefined ,在created中console了this.foodIndex,结果正常,我直接传数值给setCart:
this.addCart(0)
依然会报错:this.foodIndex is undefined
会不会我在store.js注册的mutations方法不对?
largeQ
qq_笑_17