0

I'm trying to store data into local storage with Vue.js.

If I go into my main.js file and write localStorage.set('something', 5). I can see the data in my Chrome dev tools under 'Storage' in the 'Application' panel.

I want this to happen with my other data (the data I actually plan on using) but I don't see it. However, this code is stored under 'methods: {}' in my App.vue parent component. I'm gonna assume this is the problem but as I'm new to Vue I have no idea where to put my code so it will display in local storage in dev tools.

I'm displaying my code here to show its location, not its functionality.

// App.vue
data() {
  return {
    healthScore: 0,
    storedHealthScore: 0
  }
},
methods: {
  setHealthScore() {
    localStorage.setItem('HealthScore', this.healthScore)
},
  getHealthScore() {
    this.storedHealthScore = localStorage.getItem('HealthScore')
},
  removeHealthScore() {
    localStorage.removeItem('HealthScore')
  }
}

Where do I need to place my code so that I will see this data in local storage?

1 Answer 1

1

It doesn't matter in which file your are storing data. It's is available throughout the your website.And also make sure your setHealthScore() is working correctly.

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

6 Comments

I don't think the file matters but the location in the file. I don't know how to make sure setHealthScore() is working. Obviously console.log, but I don't know where console.logs should be placed in Vue.js apps.
setHealthScore() { console.log('button clicked!') localStorage.setItem('HealthScore', this.healthScore) }, now please make sure you function is working correctly
Yeah, I've tried it. Nothing appears in the console. For whatever reason, I can't get local storage functions to work under methods: {}.
Where are you calling setHealthScore() function from? also, remove return from data {} without it is working fine.
Under methods: {} in App.vue. It shows up top. I need data() there for other things. I didn't post all my data() properties on here because they're not relevant.
|

Your Answer

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