拉丁的傳說(shuō)
2018-08-31 18:31:19
在mutations中注冊(cè)的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
}
},結(jié)果會(huì)報(bào)錯(cuò),this.foodIndex is undefined ,在created中console了this.foodIndex,結(jié)果正常,我直接傳數(shù)值給setCart:this.addCart(0)依然會(huì)報(bào)錯(cuò):this.foodIndex is undefined會(huì)不會(huì)我在store.js注冊(cè)的mutations方法不對(duì)?
2 回答

qq_笑_17
TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超7個(gè)贊
首先,貼一下 你定義 this.foodIndex
的代碼,確定你有定義這個(gè)變量
其次,貼出來(lái)的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
}
}
如果還有問題,請(qǐng)貼出更詳細(xì)的代碼
添加回答
舉報(bào)
0/150
提交
取消