如何从另一个 vuex 模块访问 getter? [英] How to access the getter from another vuex module?
问题描述
在 vuex getter 中,我知道可以像这样从另一个 vuex 模块访问状态:
Within a vuex getter I know it is possible to access the state from another vuex module like so:
pages: (state, getters, rootState) => {
console.log(rootState);
}
如何从另一个 vuex 模块而不是状态访问 getter?
How can I access a getter from another vuex module instead of the state though?
我有另一个名为 filters 的 vuex 模块需要访问,我已经尝试过:
I have another vuex module called filters that I need to access, I have tried this:
rootState.filters.activeFilters
其中 activeFilters
是我的吸气剂,但这不起作用.使用 rootState.filters.getters.activeFilters
也不起作用.
Where activeFilters
is my getter but this does not work. using rootState.filters.getters.activeFilters
also does not work.
推荐答案
不得不翻阅文档,但我找到了:
Had to dig through the documentation but I found it:
https://vuex.vuejs.org/en/api.html
(Ctrl+F 在该页面上搜索 RootGetters)
(Ctrl+F search for RootGetters on that page)
我的代码变成:
pages: (state, getters, rootState, rootGetters) => {}
请注意,所有的 rootGetter 都是全局的,您不再像使用 rootState 那样使用它,在这种情况下,您将使用模块名称作为状态的前缀.
Beware that all rootGetters are global and you no longer use it like rootState where you would prefix the state by the module name.
您只需像这样从另一个模块调用 getter:
You simply call a getter from another module like so:
rootGetters.activeFilters
希望这能帮助将来遇到此问题的人.
Hopefully this will help someone out in the future who is running into this as well.
这篇关于如何从另一个 vuex 模块访问 getter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!