我正在学习vuex,并取得了一些进展,但我被困在了一些事情上。我有一个包含多个模块的存储区的vue组件。我有带设备的地点。用户选择一个位置,而我将其存储在我的vuex状态中的location模块中。
我试图从设备模块访问该位置,以便只加载所选位置的设备。
文件上说,这应该是可行的:
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",
}
}
}但是我不知道如何访问位置/位置,我可以通过访问rootGetters访问context.rootGetters。
有什么建议吗?
发布于 2018-12-13 05:21:08
基于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');
}
}
}https://stackoverflow.com/questions/53754966
复制相似问题