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

How to use localStorage with vue? #2037

Closed
seemsindie opened this issue Dec 16, 2015 · 3 comments
Closed

How to use localStorage with vue? #2037

seemsindie opened this issue Dec 16, 2015 · 3 comments

Comments

@seemsindie
Copy link

I posted on chat(twice), nobody answered, so i am hoping i can get some help here.

I have a mixin isLoggedIn() that return true if item is present in localStorage and false if it isn't.
My question is, how can i re-evaluate the state when i update or remove item form localStorage, because i have some elements shown only if isLoggedIn is true?

And is it right to use v-show="isLoggedIn()" or would you suggest something else, i don't know what is the best practice for this?

Thanks guys.

@yyx990803
Copy link
Member

The catch here is that localStorage is not reactive, so you need something that is reactive to trigger the changes. Basically, you use an object to represent the login state, and when you login/logout, you'd modify localStorage and that object together (and initialize that object by reading from localStorage on app load).

See http://vuejs.org/guide/application.html#State_Management
and https://auth0.com/blog/2015/11/13/build-an-app-with-vuejs/ which does exactly what you are trying to do.

@seemsindie
Copy link
Author

@yyx990803 Thanks man, this article is exactly what i am trying to do.

@timwis
Copy link

timwis commented Nov 5, 2017

Where's the best place to check local storage at app initialization to see if the user's already logged in from a previous session? In the tutorial referenced, they just have a line inside the startup file / main.js before Vue is initialised. I've done something similar. Is that recommended?

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