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);
    }
  },

  

原文地址:https://www.cnblogs.com/aidixie/p/14981568.html