How to configure Vue mapActions

自古美人都是妖i 提交于 2019-12-04 19:45:32

mapActions is used in a component's methods property.

// my-component.vue
import { mapActions } from 'vuex'

export default {
    ...
    methods: {
        ...mapActions('namespaced/module', [
            'myAction',
            'myOtherAction'
        ])
    }
}

The namespace can determined by the module's filename. For example, given a file - moduleA.js - getters, mutations, actions would be namespaced as moduleA/someGetter, moduleA/someAction, moduleA/someMutation.

...mapActions('moduleA', [
    'someAction',
    'anotherAction'
])

When the module is registered, all of its getters, actions and mutations will be automatically namespaced based on the path the module is registered at

The other way is using the registerModule method, which allows for dynamic runtime registration:

// register a module `myModule`
store.registerModule('myModule', {
  // ...
})

// register a nested module `nested/myModule`
store.registerModule(['nested', 'myModule'], {
  // ...
})

Vuex Docs - Namespacing

Vuex Docs - Dynamic Module Registration

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!