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')`
        }),
    }
}

  


免責聲明!

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



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