gpt4 book ai didi

vue.js - 从另一个模块访问 vuex 模块状态

转载 作者:行者123 更新时间:2023-12-03 06:43:36 24 4
gpt4 key购买 nike

我正在学习 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 未定义。

如果我提醒上下文,我可以看到 rootGetters
{
"getters": {},
"state": {
"equipments": [],
"equipment": null
},
"rootGetters": {
"locations/LOCATIONS":[
{
"id": 1,
"name": "West Pico",
}
}
}

但是我不知道如何访问位置/位置,我可以通过访问 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');
}
}
}

关于vue.js - 从另一个模块访问 vuex 模块状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53754966/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com