Jakob Eriksen
Sep 5, 2018 · 1 min read

Thanks for sharing — very useful :-)

If you combine it with a global instance (using Vue.prototype) it is super easy to share it between components (sorry if any code typos ;-)

Vue.prototype.$storage = new Vue({
data: {
todos: JSON.parse(localStorage.getItem('todos') || '[]')
},
watch: {
todos: {
handler(){
localStorage.setItem('todos', JSON.stringify(this.todos));
}, deep: true }
}
}
});

Vue.component('vue-list-todos', {
template: `<ul>
<li v-for="todo in todos">{{ todo.text}}</li>
</ul>`,
data(){ return {
todos: this.$storage.todos
}}
});
...

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade