Vuex |如何在模块操作中提交全局突变? [英] Vuex | How to commit a global mutation in a module action?

查看:31
本文介绍了Vuex |如何在模块操作中提交全局突变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在命名空间模块中有一个操作和一个全局突变(即不在模块中).我希望能够在操作中提交全局突变.

I have an action in a namespaced module and a global mutation (i.e. not in a module). I would like to be able to commit the global mutation inside the action.

// Global mutation
export default {
  globalMutation (state, payload) {
    ...
  }
}

// Action in a namespaced module
export default {
  namespaced: true,

  actions: {
    namespacedAction ({ commit, dispatch, state }, payload) {
      commit({ type: 'globalMutation' })
    }
  }
}

在调度命名空间操作时,Vuex 显示:

When the namespaced action is dispatched, Vuex displays:

[vuex] unknown local mutation type: globalMutation, global type: module/globalMutation

有没有我可以传递给 commit 函数来调用这个全局突变的选项?

Is there an option I can pass to the commit function to call this global mutation?

推荐答案

看起来我刚刚找到了一种使用 { root: true } 参数的方法.

Looks like I just found a way with the { root: true } parameter.

commit('globalMutation', payload, { root: true })

<小时>

如果模块是命名空间的,请改用全局路径:


If module is namespaced, use global path instead:

commit('module/mutation', payload, { root: true })

这篇关于Vuex |如何在模块操作中提交全局突变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆