【问题标题】:How to configure Vue mapActions如何配置 Vue mapActions
【发布时间】:2018-03-29 05:32:57
【问题描述】:

vue-cli 商店

我的代码是这样的: ...mapActions('一些/嵌套/模块',[ 'getCountry', '获取货币' ]),

如何在Vue组件中设置mapActions路径?

【问题讨论】:

    标签: vue.js vuex


    【解决方案1】:

    mapActions 用于组件的methods 属性中。

    // my-component.vue
    import { mapActions } from 'vuex'
    
    export default {
        ...
        methods: {
            ...mapActions('namespaced/module', [
                'myAction',
                'myOtherAction'
            ])
        }
    }
    

    命名空间可以由模块的文件名决定。例如,给定一个文件 - moduleA.js - getter、mutations、actions 将命名为 moduleA/someGettermoduleA/someActionmoduleA/someMutation

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

    当模块被注册时,它的所有getter、actions和mutations都会根据模块注册的路径自动命名空间

    另一种方法是使用registerModule 方法,它允许动态运行时注册:

    // 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

    【讨论】:

    • 如何设置命名空间? export default new Vuex.Store({ modules: { account: { namespaced: true, modules: { user, set } } } }) ;
    猜你喜欢
    • 2020-05-22
    • 2020-03-02
    • 2019-12-22
    • 2020-11-22
    • 2021-07-04
    • 2020-07-18
    • 2017-08-10
    • 2018-07-03
    • 2018-08-12
    相关资源
    最近更新 更多