Ste*_*n-v 40 javascript vue.js vuex vuejs2
在vuex getter中我知道可以从另一个vuex模块访问状态,如下所示:
pages: (state, getters, rootState) => {
    console.log(rootState);
}
如何从另一个vuex模块而不是状态访问getter?
我有另一个名为过滤器的 vuex模块,我需要访问,我试过这个:
rootState.filters.activeFilters
activeFilters我的吸气剂在哪里,但这不起作用.使用rootState.filters.getters.activeFilters也行不通.
Ste*_*n-v 83
不得不仔细阅读文档,但我发现它:
https://vuex.vuejs.org/en/api.html
(Ctrl + F在该页面上搜索RootGetters)
我的代码变成:
pages: (state, getters, rootState, rootGetters) => {}
请注意,所有rootGetter都是全局的,您不再像rootState那样使用它,您可以使用模块名称为状态添加前缀.
你只需从另一个模块调用一个getter,如下所示:
rootGetters.activeFilters
希望这将有助于将来遇到这种情况的人.
Kir*_*iya 24
如果你想getter从模块访问全局/命名空间,你可以这样做,
getters: {
    // `getters` is localized to this module's getters
    // you can use rootGetters via 4th argument of getters
    someGetter (state, getters, rootState, rootGetters) {
        rootGetters.someOtherGetter //'someOtherGetter' global getter
        rootGetters['bar/someOtherGetter'] //'bar/someOtherGetter' namespaced getter
    },
    ...
},
这里是访问方式actions,也包括用法action和mutations参考。
actions: {
      // dispatch and commit are also localized for this module
      // they will accept `root` option for the root dispatch/commit
      someAction ({ dispatch, commit, getters, rootGetters }) {
      
        rootGetters.someGetter //'someGetter' global getter
        rootGetters['bar/someGetter'] //'bar/someGetter' namespaced getter
        dispatch('someOtherAction') //'someOtherAction' local action
        dispatch('someOtherAction', null, { root: true }) //'someOtherAction' namespaced action
        commit('someMutation') //'someMutation' local mutation
        commit('someMutation', null, { root: true }) //'someMutation' namespaced mutation
      },
      ...
    }
  }
| 归档时间: | 
 | 
| 查看次数: | 19624 次 | 
| 最近记录: |