vuex icon indicating copy to clipboard operation
vuex copied to clipboard

Retrieve module context from store instance

Open mspiderv opened this issue 6 years ago • 2 comments

What problem does this feature solve?

This feature adds the ability to retrieve the store module context directly from root store instance.

What does the proposed API look like?

Let's suppose, that store variable is my Vuex (root) store instance.

I solve this problem with the following code:

let subModuleContext = store._modulesNamespaceMap['subModuleName/'].context

It would be nice to write

let subModuleContext = store.getContext('subModuleName')

Then, I can write subModuleContext.dispatch('someAction') instead of store.dispatch('subModuleName/someAction')

mspiderv avatar May 31 '19 01:05 mspiderv

I got bitten by the way actions and states are handled in modules (quite differently). I think this problem (context) can be solved in a better way that provides a more consistent usage of the whole api.

current state accessing in submodule

store.state.modulaA.someValue

current action accessing in submodule

store.dispatch('moduleA/someAction')

expected

store.action.ModuleA.someAction()

And to be honest a more straight forward way for both cases would be:

store.modules.moduleA.[state|action].[someValue|someAction()]

With the registration implemented maybe like (poc):

let module = 'ModuleA'
let actions = {/*...*/}
store.modules[module] = {
 state: store.state[module],
 action: Object.assign({}, ...Object.keys(actions).map( action=>{o={};o[action]= (arg) =>store.dispatch(module + '/' + action);return o}))
}

estani avatar Dec 10 '19 10:12 estani

You can use mapActions to access namespacedDisptach

export default {
  mounted() {
    this.dispatchSomeAction()
  },
  methods: mapActions('subModuleName', {
    dispatchSomeAction(dispatch) {
      dispatch('someAction')
    },
  }),
}

PatrykWalach avatar Sep 08 '20 11:09 PatrykWalach