Cha*_*y57 5 getter module vue.js vuex
我正在 VueJS 中构建一个应用程序,但是 Vuex 模块给我带来了一些问题。
无论我尝试什么,当从另一个模块调用它时,我都无法检索模块中的状态。
在我的 Vue Devtools Vuex getter 中:modA/mycustomer:未定义
我已经阅读了 Vuex 网站上的所有文档,并尝试了 stackoverflow 中已经给出的一些解决方案,但我迷路了。大概是在俯视什么
我的 store.js
export default new Vuex.Store({
modules: {
modA: moduleA,
modB: moduleB
}
})
Run Code Online (Sandbox Code Playgroud)
模块A JS
export default {
namespaced: true,
state:{
customerId:0
},
getters: {
customerId: state => {
return state.customerId
}
}
}
Run Code Online (Sandbox Code Playgroud)
模块B.js
export default {
namespaced: true,
state:{
orderId:0
},
getters: {
orderId: state => {
return state.orderId
},
mycustomer: rootGetters => {
return rootGetters['modA/customerId']
}
}
}
Run Code Online (Sandbox Code Playgroud)
我通常希望通过此请求获得 customerId。但只有 undefined 一些状态是需要跨模块的。
如果你想使用全局状态和 getter,
rootState并且rootGetters作为第三个和第四个参数传递给 getter 函数,并且还作为传递给动作函数的上下文对象的属性公开。
所以如果要customerId在modA模块中的mycustomergetter里面访问模块的getter modB,应该是:
mycustomer(state, getters, rootState, rootGetters) {
return rootGetters['modA/customerId']
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2661 次 |
| 最近记录: |