vuex mutations 报错?
本文介绍了vuex mutations 报错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
在mutations中注册的setCart方法:
//store.js
mutations:{
setCart(state,foodIndex){
Vue.set(state.goods.foods[foodIndex],"count",1);
}
}
组件中提交mutations并使用setCart方法
//childComponent.vue
methods:{
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方法不对?
解决方案
首先,贴一下 你定义 this.foodIndex
的代码,确定你有定义这个变量
其次,贴出来的methods的代码有问题
建议改为
methods:{
setCart (foodIndex){
this.$store.commit('setCart', foodIndex);
},
addCartItem (event) {
if (!this.food.count){
this.setCart(this.foodIndex);
}else{
this.addCart(this.foodIndex)
return
}
}
如果还有问题,请贴出更详细的代码
这篇关于vuex mutations 报错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文