从另一个模块访问 vuex 模块状态

Dyl*_*ler 3 vue.js vuex vuejs2 vuex-modules

我正在学习 vuex 并取得了一些进展,但我遇到了一些问题。我有一个包含许多模块的商店的 vue 组件。我有设备的地方。用户选择一个位置,我将它存储在我的 vuex 状态中的位置模块中。

我正在尝试从设备模块访问该位置以仅加载所选位置的设备。

文档说这应该有效:

actions: {
incrementIfOddOnRootSum ({ state, commit, rootState }) {
  if ((state.count + rootState.count) % 2 === 1) {
    commit('increment')
  }
}
Run Code Online (Sandbox Code Playgroud)

当我在我的模块中尝试这个时:

GET_EQUIPMENTS : async (context,payload, rootState) => {
  alert(JSON.stringify(rootState.location, null, 4));
}
Run Code Online (Sandbox Code Playgroud)

我收到错误 rootState 未定义。

如果我提醒上下文,我可以看到 rootGetters

{
  "getters": {},
  "state": {
    "equipments": [],
    "equipment": null
  },
  "rootGetters": {
    "locations/LOCATIONS":[
    {
       "id": 1,
       "name": "West Pico",
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

但是我不知道如何访问位置/位置,我可以通过访问 context.rootGetters 来访问 rootGetters。

有什么建议吗?

Max*_*xim 8

基于https://vuex.vuejs.org/guide/modules.html

你应该换 state, commit, rootState{..}采取行动:

actions: {
    incrementIfOddOnRootSum({ state, commit, rootState }, yourParam) {
      if ((state.count + rootState.count + yourParam) % 2 === 1) {
        commit('increment');
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)