如何使用来自另一个模块的模块内的 getter Vuex 状态
How to Vuex state with a getter within a module from another module
我正在用 VueJS 构建应用程序,但是 Vuex 模块给我带来了一些问题。
无论我怎么尝试,当从另一个模块调用它时,我都无法检索模块中的状态。
在我的 Vue Devtools Vuex getter 中:
modA/mycustomer: 未定义
我已经阅读了 Vuex 网站上的所有文档,并尝试了一些已经在 Whosebug 中给出的解决方案,但我迷路了。可能忽略了什么
我的store.js
export default new Vuex.Store({
modules: {
modA: moduleA,
modB: moduleB
}
})
moduleA JS
export default {
namespaced: true,
state:{
customerId:0
},
getters: {
customerId: state => {
return state.customerId
}
}
}
moduleB.js
export default {
namespaced: true,
state:{
orderId:0
},
getters: {
orderId: state => {
return state.orderId
},
mycustomer: rootGetters => {
return rootGetters['modA/customerId']
}
}
}
我通常希望通过此请求获得 customerId。但只有未定义
跨模块需要一些状态。
相关文档是 Accessing Global Assets in Namespaced Modules 其中说:
If you want to use global state and getters, rootState
and rootGetters
are passed as the 3rd and 4th arguments to getter functions, and also exposed as properties on the context object passed to action functions.
所以如果你想在modB
模块中的mycustomer
getter里面访问modA
模块的customerId
getter ,应该是:
mycustomer(state, getters, rootState, rootGetters) {
return rootGetters['modA/customerId']
}
我正在用 VueJS 构建应用程序,但是 Vuex 模块给我带来了一些问题。
无论我怎么尝试,当从另一个模块调用它时,我都无法检索模块中的状态。
在我的 Vue Devtools Vuex getter 中: modA/mycustomer: 未定义
我已经阅读了 Vuex 网站上的所有文档,并尝试了一些已经在 Whosebug 中给出的解决方案,但我迷路了。可能忽略了什么
我的store.js
export default new Vuex.Store({
modules: {
modA: moduleA,
modB: moduleB
}
})
moduleA JS
export default {
namespaced: true,
state:{
customerId:0
},
getters: {
customerId: state => {
return state.customerId
}
}
}
moduleB.js
export default {
namespaced: true,
state:{
orderId:0
},
getters: {
orderId: state => {
return state.orderId
},
mycustomer: rootGetters => {
return rootGetters['modA/customerId']
}
}
}
我通常希望通过此请求获得 customerId。但只有未定义 跨模块需要一些状态。
相关文档是 Accessing Global Assets in Namespaced Modules 其中说:
If you want to use global state and getters,
rootState
androotGetters
are passed as the 3rd and 4th arguments to getter functions, and also exposed as properties on the context object passed to action functions.
所以如果你想在modB
模块中的mycustomer
getter里面访问modA
模块的customerId
getter ,应该是:
mycustomer(state, getters, rootState, rootGetters) {
return rootGetters['modA/customerId']
}