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

Accessing vuex module state from another module

我正在学习 vuex 并取得了一些进展,但我被困在某些事情上。我有一个 vue 组件,其商店包含许多模块。我有地点有设备。用户选择一个位置,我将其存储在位置模块中的 vuex 状态中。

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

文档说这应该有效:

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

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

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

我收到错误 rootState is not defined。

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

{
  "getters": {},
  "state": {
    "equipments": [],
    "equipment": null
  },
  "rootGetters": {
    "locations/LOCATIONS":[
    {
       "id": 1,
       "name": "West Pico",
    }
  }
}

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

有什么建议吗?

基于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');
      }
    }
  }