user2410266
user2410266

Reputation: 551

Pass LocalStorage Object to Mounted data variable

I would like to pass LocalStorage Object to mounted data variable. How can I retrieve the values from object and pass into variable. Any suggestion will be really appreciated.

  mounted() {

  if (localStorage.login.user) {
  this.user= localStorage.login.user;
  }
  if (localStorage.login.email) {
  this.email = JSON.parse(localStorage.login.email);
  }
},
  var login = { user: this.user, email: this.email };
  localStorage.setItem("login", JSON.stringify(login));

Upvotes: 0

Views: 44

Answers (1)

Attila Antal
Attila Antal

Reputation: 821

The approach you were trying is the correct one. You just need to make sure that the variables this.user and this.email do exists and have values.

Use the setItem() method to store values, and getItem() to access the values. Values that are stored as stringified objects, they need to be parsed when accessed.

enter image description here

See also: Storing Objects in HTML5 localStorage Stackoverflow article.

More information about the localStorage, you can check out the Window.localStorage documentation.

Upvotes: 1

Related Questions