【问题标题】:Vuex actions, vuex modules and mapActions with custom name具有自定义名称的 Vuex 动作、vuex 模块和 mapActions
【发布时间】:2019-04-18 12:13:35
【问题描述】:

我一直在寻找如何在模板中使用 Vuex 操作而不输入所有操作名称,这就是我的代码的样子:

export default new Vuex.Store({ modules: articles, auth, blabla})

我的articles.module.js 包含动作、getter 等,其中一个动作如下所示:

[ArticleActions.remote.FETCH_ALL]({commit}) {something axios stuff}

使用命名空间为 true 导出:

export const articles = {
namespaced: true,
state: initialState,
mutations,
actions,
getters
};

在我的组件 ArticleList.vue 中,我想将该操作与 mapActions 一起使用:

 methods: {
        ...mapActions('articles', [ArticleActions.remote.FETCH_ALL])
 }

这可行,但我不想在我的模板中使用 ArticleActions.remote.FETCH_ALL 的值我想要的是

methods: {
        ...mapActions('articles', [{fetchAll: ArticleActions.remote.FETCH_ALL}])
 }

所以我只需要:

mounted(){fetchAll();}

而不是

mounted(){ArticleActions.remote.FETCH_ALL();}

我们能做到吗?

【问题讨论】:

    标签: vue.js vuex vuex-modules


    【解决方案1】:

    经过一段时间我自己想通了,实际上很容易......

    ...mapActions('articles', {
                    fetchAll: ArticleActions.remote.FETCH_ALL,
                }
            ),
    

    我卡住了,因为我习惯了另一种语法:

    ...mapGetters("articles", [
                'articles',
            ])
    

    所以我只尝试使用 [],但解决方案是使用对象,希望对您有所帮助,抱歉。 现在,如果我们这样做,它会起作用:

    mounted(){this.fetchAll();}
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-24
    • 2018-08-23
    • 2020-09-27
    • 2021-05-17
    • 2019-01-21
    • 2018-06-03
    • 2018-09-22
    • 2019-08-23
    相关资源
    最近更新 更多