vuex使用modules namespaced 后,模块名不同,函数名相同时候在组件中分发Action

你在组件中使用 this.$store.dispatch('xxx') 分发 action,或者使用 mapActions 辅助函数将组件的 methods 映射为 store.dispatch 调用(需要先在根节点注入 store):

import { mapActions } from 'vuex'

export default {
    // ...
    methods: {
        ...mapActions([
        'increment', // 将 `this.increment()` 映射为 `this.$store.dispatch('increment')`

        // `mapActions` 也支持载荷:
        'incrementBy' // 将 `this.incrementBy(amount)` 映射为 `this.$store.dispatch('incrementBy', amount)`
        ]),
        ...mapActions({
          add: 'increment' // 将 `this.add()` 映射为 `this.$store.dispatch('increment')`
        }),
        ...mapActions({
          add1: 'user/increment', // 将 `this.add()` 映射为 `this.$store.dispatch('user/increment')`
        add2: 'depart/increment' // 将 `this.add()` 映射为 `this.$store.dispatch('depart/increment')`
        }),
    }
}

  

原文地址:https://www.cnblogs.com/zzsdream/p/10568417.html