SHARE
TWEET

Untitled

a guest Jul 16th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Store, Module, ActionContext } from 'vuex';
  2.  
  3. export function createModuleActionContext<RootState, ModuleState>(
  4.   rootStore: Store<RootState>, moduleStore: Module<ModuleState, RootState>, moduleName: string
  5. ): ActionContext<ModuleState, RootState>  {
  6.   return {
  7.     get dispatch() {
  8.       return moduleStore.namespaced
  9.         ? (rootStore as any)._modulesNamespaceMap[moduleName + '/'].context.dispatch
  10.         : rootStore.dispatch;
  11.     },
  12.     get commit() {
  13.       return moduleStore.namespaced
  14.         ? (rootStore as any)._modulesNamespaceMap[moduleName + '/'].context.commit
  15.         : rootStore.commit;
  16.     },
  17.     // @ts-ignore
  18.     state: moduleStore.state,
  19.     getters: moduleStore.getters,
  20.     rootState: rootStore.state,
  21.     rootGetters: rootStore.getters
  22.   };
  23. }
  24.  
  25. export function createRootActionContext<RootState>(
  26.   { state, getters, commit, dispatch }: Store<RootState>
  27. ): ActionContext<RootState, RootState> {
  28.   return {
  29.     dispatch,
  30.     commit,
  31.     state,
  32.     getters,
  33.     rootState: state,
  34.     rootGetters: getters
  35.   };
  36. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top