[Vuejs]-Todo app: getting todo-items in and out of localstorage

0👍

When you do myStorage.setItem("todo", JSON.stringify(this.title)), you’re saving only the title in localStorage at key "todo". If you later call that same line of code again, you’ll be overwriting the value.

If you’re saving TODO item titles, they will all need their own key:

myStorage.setItem("todo-key1", todo1.title)

Of you could save them all as a collection:

myStorage.setItem("todos", JSON.stringify([todo1.title, todo2.title]))

but then adding a new todo involves reading the existing value before setting it:

myStorage.setItem("todos", JSON.stringify(JSON.parse(myStorage.getItem("todos")).push(newTitle)))

Helper functions can make this all easier to read and cleaner of course.

Leave a comment