为什么 Vuex 模块中的这个简单 getter 无法做到 API 参考所说的?
const getters = {
myGetRootState: (state, rootState) => {
return rootState
}
}
上面的 getter 返回当前模块状态,而不是存储状态。
而这个
const getters = {
myGetRootState: rootState => {
return console.log(rootState.anyModuleYouWant)
}
}
日志未定义。我已经尝试了许多上下文和返回值的排列,但无济于事。
知道为什么会这样以及如何让它做预期的事情吗?
模块 getter 需要 3 个参数(本地状态、getter、根状态(。
const getters = {
myGetRootState (state, getters, rootState){
console.log("rootState.exampleVar = "+ rootState.exampleVar)
return rootState.exampleVar
}
}
在 Vue 实例组件中使用:
computed: {
myGetRootState: function(){
return this.$store.getters.myGetRootState
}
}
您无需在 Vue 实例中指定模块。