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