championswimmer / vuex-persist

A Vuex plugin to persist the store. (Fully Typescript enabled)
http://championswimmer.in/vuex-persist
MIT License
1.67k stars 116 forks source link

Calling unregisterModule does not remove that module's data from localStorage? #80

Open rabbitfufu opened 5 years ago

rabbitfufu commented 5 years ago

Hello,

I am new to Vuex, so apologies if I am overlooking something obvious. I have a question about the behaviour of this package...

In my app, when a user logs in, I use registerModule to attach the appropriate modules for that user to the vuex store. When they logout, I use unregisterModule on those same modules.

It appears that calling unregisterModule, while it removes the module from my store, does not actually remove the module's data from the localStorage created by vuex-persist. So if I click refresh, suddenly that module's state reappears in my store, even though the module is no longer a part of the store.

For example:

registerModule('profile', profile) --> state: { profile: {...}}

unregisterModule('profile') --> state: {}

However, after a refresh, state once again looks like: state: { profile: {...}}

I guess I could call a reset mutation on the profile module before unregistering it, but even then, on a refresh, I would still have an empty profile object in my state. I would prefer for 'profile' to be gone from localStorage entirely, since it has been removed from the store.

Any advice on how to accomplish this? Thanks (in advance) for your input!

rabbitfufu commented 5 years ago

Oh hey, I figured it out. Looks like calling unregisterModule doesn't trigger an update in Vuex. So if the first thing you do is click refresh after calling unregisterModule, then the state from that module will be restored. However, if you commit a mutation immediately after unregistering...

unregisterModule('profile') store.dispatch('anycommit')

...and then click refresh, it behaves as expected. So that's an easy workaround. (Not sure if there is a way to trigger an update event without committing a mutation...)