Delete persisted state #88
Comments
|
The recommended way here is to stay away from the localStorage entry. If you would like the remove any "user" related state, you can do that with a mutation on your state. This keeps all state related issues to a bare minimum in your application. |
|
After deep digging in internet I found a clean solution which works well. Based on these 2 answers (#1 #2) I made a workable code. My structure of Vuex's Inside each module we need to move all states into separated var In Vue component we can use it like: |
|
Thanks! But if I have lots of modules, do I need to call such reset methods for all of my modules inside logout() method (which is also is a method inside auth module)? |
|
Hello everybody, I think its better to use the reducer method to remove all the vuex state. |
|
@vkatsar Just two remarks for other people trying this. In my setup I had to remove the "(store)" to make it work. Also it's important to know that the state will only be removed after a page refresh, so my logout action looks like this: Action: Mutation: and the reducer: |
|
@tcamde how does you "logOutUser" look? Is this needed? It seems that the answer is not complete. commit('logOutUser') // this will cause the vue-persist-store plugin to set all state to empty |
|
I was able to get this working with the reducer method and This thread should probably be posted on the readme |
|
Can someone please provide a full example here of how to delete the persisted state. All of the above make reference to code that isn't visible to the Reader, so there is no way to connect the dots and understand how to actually use reducer. For example, what is |
|
I updated my example with the full code, though it might be a bit specific to my use-case |
|
@tcamde I appreciate the update! had a couple more questions for you...
reducer(val){
if(val.authentication.isUserLoggedIn === false) ...
}
|
|
Thanks @tcamde, this really helped:
In my case, So, to answer @uxinkc's second question: yes, |
|
Sorry to bother you, but is it necessary to refresh the page to logout? I have the same problem, but 'location.reload' will lead to a while of blank white screen in front of user. Is there any better plan by now? |
I try the way to reset state in vuex modules when user logout, but other feature of my program not work。 |
best option available out there... |
Do you want to request a feature or report a bug?
Feature
What is the current behavior?
There is no way to delete the persisted state programmatically, there are use cases for removing the state in local storage.
If the current behavior is a bug, please provide the steps to reproduce.
What is the expected behavior?
We should be able to remove the state from local storage either with
localStorage.removeItem('vuex')or something likethis.$store.clear.If this is a feature request, what is motivation or use case for changing the behavior?
When my users log out I want to remove all of the state data, this way when a different person logs in none of the existing data is shared.
I tried to do this with
localStorage.removeItem('vuex')however I see that in #37 it's commented that the plugin gets there first and the state is rewritten again.It's possible to do it manually, however this would mean setting all of the states to default values in all of the modules as far as I can tell which is not very efficient.
The text was updated successfully, but these errors were encountered: