maoberlehner / how-to-structure-a-complex-vuex-store

This is an example project for the following article: https://markus.oberlehner.net/blog/how-to-structure-a-complex-vuex-store/
187 stars 36 forks source link

persistedState #1

Closed alsconsulting closed 6 years ago

alsconsulting commented 6 years ago

great example! really helped me. But I have a problem when I use persisted state. I want the entire state to persist over refresh. So I use the vuex-persistedState plugin - eg: `Vue.use(Vuex)

export default new Vuex.Store({ strict: true, plugins: [ createPersistedState() ],`

But when I do this I end with with all sorts of errors: eg. [vuex] module namespace not found in mapGetters():

As soon as I remove the plug in all is fine - except you can't hit F5.

Any ideas?

maoberlehner commented 6 years ago

Hey @alsconsulting there are some quirks with persistedState and dynamically registered modules, but you can checkout this issue: https://github.com/robinvdvleuten/vuex-persistedstate/issues/100

Please let me know if this works for you. Thx.

alsconsulting commented 6 years ago

Hi Markus,

No joy – I actually just took your project and added the persisted-state and the {preserverState: true} to the module registration but it doesn’t work unfortunately.

Very frustrating!

From: Markus Oberlehner notifications@github.com Sent: Monday, 26 March 2018 1:32 PM To: maoberlehner/how-to-structure-a-complex-vuex-store how-to-structure-a-complex-vuex-store@noreply.github.com Cc: Andrew Smith andrew.smith@alsconsulting.com.au; Mention mention@noreply.github.com Subject: Re: [maoberlehner/how-to-structure-a-complex-vuex-store] persistedState (#1)

Hey @alsconsultinghttps://github.com/alsconsulting there are some quirks with persistedState and dynamically registered modules, but you can checkout this issue: robinvdvleuten/vuex-persistedstate#100https://github.com/robinvdvleuten/vuex-persistedstate/issues/100

Please let me know if this works for you. Thx.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHubhttps://github.com/maoberlehner/how-to-structure-a-complex-vuex-store/issues/1#issuecomment-376036861, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AJt-zg3jN_K4E6juoSGrlRTS-yluJNlUks5tiGETgaJpZM4S6aKc.

maoberlehner commented 6 years ago

Hmm, I understand that this is a frustrating situation. But I don't see what I can do about it. The technique described in my blog article and featured in this repository, works as it is.

As I see it, it's a problem with the vuex-persistedstate package. I'd suggest to make your case in the issue I've linked above.

Thx.

alsconsulting commented 6 years ago

Ended up declaring and instantiating everything at the root level. Seemed to be a mixture of the dynamic registering and trying to use three levels. Ie. Module within a module.

maoberlehner commented 6 years ago

Hey @alsconsulting

I'm sorry, I completely misunderstood the problem. I took a closer look and I realized it's not a problem with the vuex-persistedstate package at all, but with how I check if the module was already registered. Unfortunately there is no "clean" way of how to check if a module was already registered, so the way I solved it, is a little hacky (more about that problem: https://github.com/vuejs/vuex/issues/833).

This is what I've done to make it work: https://github.com/maoberlehner/how-to-structure-a-complex-vuex-store/commit/9018a9b014c4086a8beb3c99528a8e50fd305a46

const shippingAddressModuleRegistered = store._modules.root._children.shippingAddress !== undefined;
const shippingAddressStateExists = store.state.shippingAddress;

// Previously I've checked if the state was set to determine if the component was registered
// but with persistedstate, the state is set although the component is not registered yet.
// The new method above, checks if the module is registered for real.
if (!shippingAddressModuleRegistered) {
  // The preserveState option is only set to true, if there already is a state to preserve.
  // So for the first time the page is loaded, there is no state and we set the
  // preserveState option to `false` on further page loads, the persistedState kicks in,
  // so there already is a state and we set preserveState to `true`.
  store.registerModule(`shippingAddress`, shippingAddress, { preserveState: shippingAddressStateExists });
}
maoberlehner commented 6 years ago

You can checkout the feature/vuex-persisted-state branch to see it work with vuex-persistedstate. Thanks for your patience and for raising this issue. I'm happy this problem is solved.