首页 > 解决方案 > 在 Vuex 中可以跳过操作并直接从组件提交突变吗?

问题描述

每次出现这个问题时,答案总是相同的——动作是为了异步。而且我知道在未来版本的 Vuex 动作和突变将被合并。

但是,嘿,我可以直接从我的组件方法中进行异步调用。例如,为什么这被认为是一种不好的做法?异步在这里不是问题。那么为什么我什至需要行动呢?

methods:
{
    async fetchTodos()
    {
        this.$store.commit('isLoading', true)

        const result = await Todos.fetchAll()

        this.$store.commit('setTodos', result.data)
        this.$store.commit('isLoading', false)
    }
},

标签: javascriptvue.jsvuex

解决方案


推荐阅读