Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't work with vuex-persist in dynamic mode #167

Closed
avxkim opened this issue Aug 20, 2019 · 1 comment
Closed

Doesn't work with vuex-persist in dynamic mode #167

avxkim opened this issue Aug 20, 2019 · 1 comment

Comments

@avxkim
Copy link

avxkim commented Aug 20, 2019

This is related to following issues:

But there's no real answer on this question, in manual mode it does work properly, but in dynamic it doesn't, because browser refresh doesn't re-apply state values from local storage. Kinda the same guy involved in both of these projects @championswimmer, but they are not compatible? :)

@dnsritch
Copy link

dnsritch commented Jan 9, 2020

mabe I have a solution for this,I write a vuex plugin by hijacking the store.registerModule in vuex
here is the code
maybe someone can make a optimization for this
I think it could be a better solution

//#region [rgba(0,205,20,0.1)]
const dynamicRegisterPlugin = (store: Store<IRootState>) => {

  // Override dynamic module handling in vuex
  let origRegisterModule: any
  if (store.registerModule) {
    origRegisterModule = store.registerModule.bind(store)
    // your module interfaces
    type T = IAppState | IUserState | IPermissionState | ITagsViewState | ISettingsState | IHome
    //@ts-ignore
    store.registerModule = (
      path: string,
      module: Module<T, IRootState>,
      options?: ModuleOptions
    ) => {
      // console.log(store)
      if ((store.state as Object).hasOwnProperty(path)) {
        origRegisterModule(path, module, { preserveState: true })
      } else {
        origRegisterModule(path, module, options)
      }
      // origRegisterModule(path, module, options)
    }
  }
}
//#endregion


const vuexLocal = new VuexPersistence<IRootState>({
  storage: window.sessionStorage
})

// Declare empty store first, dynamically register all modules later.
export default new Vuex.Store<IRootState>({
  plugins: [dynamicRegisterPlugin, vuexLocal.plugin]
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants