vuex 的模塊中如何調用 mutations 中的方法


vuex 的模塊中如何調用 mutations 中的方法

   模塊vuexTest.js

  

/**
 * 模塊 vuexTest.js
 */
 export default {
    namespaced: true,
    state: {
        stateHello: 1,
    },
    mutations: {
        mutationsHello(state, val) { // 只用兩個參數 一個時state ,另一個是可以 字符串、對象、數組等
            console.log("1111");
            console.log("val", val);
            state.stateHello += val
        }
    },
    actions: {

    }
}

  1、不使用輔助函數 mapMutations 情況下

methods: {
    changeVal() {
      this.$store.commit("vuexTest/mutationsHello", 2)
    }
  }

  2、使用輔助函數 mapMutations 情況下

  methods: {
    ...mapMutations("vuexTest", ['mutationsHello']),
    ...mapMutations("vuexTest", {
      mutationsHello: "mutationsHello"
    }),
    ...mapMutations("vuexTest", {
      changeState: "mutationsHello"
    }),
    change() {
      this.mutationsHello(2);
      this.changeState(2);
    }
  },

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM