vuex-typescript icon indicating copy to clipboard operation
vuex-typescript copied to clipboard

Call getter in another module?

Open skateborden opened this issue 7 years ago • 2 comments

From an action in one module I want to call a getter in another module. I'm able to do it with context.rootGetters["namespace/getter"] but it doesn't feel like the typescript way to do it. Trying to call the read static function I can't figure out how to get the right ActionContext to pass into the getter.

skateborden avatar Jan 10 '18 17:01 skateborden

Here's my workaround:

I needed to use accessors from one module inside of the actions of another.

As the context shape of the second module is different, I was having type errors.

Specifically, I have a main module with some actions, getters, and mutations that I needed to call from an admin module.

I needed to be able to keep the type checks everywhere, but in a way that allowed me to pass a different context (that still has the same dispatch and commit methods, etc, but different state shape), to be able to call accessors from main from inside the actions of admin, with the context of those admin actions.

To achieve that, I changed:

const {commit, read, dispatch} = getStoreAccessors<MainState, State>('');

to

const {commit, read, dispatch} = getStoreAccessors<MainState | any, State>('');

After that, all the accessors accept anything as the state/context, but keep the type declarations for the payloads.

tiangolo avatar Nov 14 '18 14:11 tiangolo

How do I access reused modules? I have a module that I use twice and currently I access it like this:

dispatch(‘moduleA/getList’) and dispatch(‘moduleB/moduleA/getList’)

I have one exported function from the module which is

const dispatchList = dispatch(actions.getList) and I use it like this dispatchList(this.$store)

meizon avatar Apr 08 '20 16:04 meizon