How to access the getter from another vuex module?

痴心易碎 提交于 2020-05-24 11:18:15

问题


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);
}

How can I access a getter from another vuex module instead of the state though?

I have another vuex module called filters that I need to access, I have tried this:

rootState.filters.activeFilters

Where activeFilters is my getter but this does not work. using rootState.filters.getters.activeFilters also does not work.


回答1:


Had to dig through the documentation but I found it:

https://vuex.vuejs.org/en/api.html

(Ctrl+F search for RootGetters on that page)

My code becomes:

pages: (state, getters, rootState, rootGetters) => {}

Beware that all rootGetters are global and you no longer use it like rootState where you would prefix the state by the module name.

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.




回答2:


This example is for accessing getter from one module into another module's action.

Below is user.js module with getter

isUserAuthenticated: state => {
  return !(state.token == null);
}

To access this in post.js module action

validateAction(vuexContext, actionDetails) {
  return new Promise((resolve, reject) => {

    if (vuexContext.rootGetters["user/isUserAuthenticated"]) {
        console.log("User is authenticated");
    } else {
        console.log("User is not authenticated");
    }
  });
...
}

Hope this is helpful.



来源:https://stackoverflow.com/questions/45999623/how-to-access-the-getter-from-another-vuex-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!