65

I am working on Markdown editor with Vue.JS, and I tried to use localStorage with it to save data but I don't know how to save new value to data variables in Vue.JS whenever the user types!

0

4 Answers 4

69

I found the solution that I was looking for. first use watch method to detect changes on variable you are storing data on like this:

watch: {
  input: function () {
    if (isLocalStorage() /* function to detect if localstorage is supported*/) {
      localStorage.setItem('storedData', this.input)
    }
  }
}

This will update variable’s value whenever user add new inputs.

Then assign the new value to variable like this:

app.input = localStorage.getItem('storedData');

And that's it :)

Sign up to request clarification or add additional context in comments.

Comments

66

You can just do following to save in localStorage

localStorage.setItem('YourItem', response.data)

You can fetch this using:

localStorage.getItem('YourItem')

To delete this from localStorage:

localStorage.removeItem('YourItem')

Comments

3

You can easily use the local storage in Vue:

To store:

localStorage.storedData = this.input;

To get the data:

let value = localStorage.storedData;

Reference: https://v2.vuejs.org/v2/cookbook/client-side-storage.html

1 Comment

actually this answer worked for me but accepted answer did not work
2

you can use v-model to bind you variable with every change or you can put it computed :{} section.computed is like life hook of vue.js it re-render the component again when it values are changed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.