vuex-typescript
vuex-typescript copied to clipboard
Call getter in another module?
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.
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.
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)