How to access the getter from another vuex module?

前端 未结 2 1139
别跟我提以往
别跟我提以往 2021-01-30 19:45

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(rootS         


        
2条回答
  •  梦如初夏
    2021-01-30 20:14

    If you want to access global/namespaced getter from the module, Here is how you can it,

    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
        },
        ...
    },
    

    Here is the way to access actions, Also includes usage of action and mutations for the reference.

    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
          },
          ...
        }
      }
    

提交回复
热议问题