- Vue.js 2 Web Development Projects
- Guillaume Chau
- 116字
- 2021-07-02 22:34:29
Saving the selection
It would be very handy if our app could select the note that was selected last time. We just need to store and load the selectedId data property used to store the ID of the selected note. That's right! Once more, we will use a watcher to trigger the save:
watch: { ... // Let's save the selection too selectedId (val) { localStorage.setItem('selected-id', val) }, }
Also, we will restore the value when the property is initialized:
data () { return { notes: JSON.parse(localStorage.getItem('notes')) || [], selectedId: localStorage.getItem('selected-id') || null, } },
It's ready! Now, when you refresh the app, it should look exactly how you left it, with the same note selected.
推薦閱讀