亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

vuex mutations 報錯?

vuex mutations 報錯?

拉丁的傳說 2018-08-31 18:31:19
在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方法不對?
查看完整描述

2 回答

?
largeQ

TA貢獻2039條經驗 獲得超8個贊

addCartItem:function (event)改成addCartItem (event)試試

查看完整回答
反對 回復 2018-09-09
?
qq_笑_17

TA貢獻1818條經驗 獲得超7個贊

首先,貼一下 你定義 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

        }

    }

如果還有問題,請貼出更詳細的代碼

查看完整回答
反對 回復 2018-09-09
  • 2 回答
  • 0 關注
  • 1352 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號