Subscribe example #443
-
İ am trying to implement auto serialising store to localstorage. From some discussions I understand that best way to do it is with But I could not find any doc or example on subscribe method. Would appreciate for any tip. Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
At the end const pinia = createPinia()
watch(pinia.state, handler, { deep: true }) The It's also at the handler level that one should check for specific stores' state as they are created on the fly as they get used. This means that when the watcher is created, it's observing an empty object ( // given defineStore({ id: 'myStore' })
watch(() => pinia.state.value.myStore, handler, { deep: true }) |
Beta Was this translation helpful? Give feedback.
-
Will this work outside of the vue instance for subscriptions? |
Beta Was this translation helpful? Give feedback.
At the end
$subscribe
is just a watcher, so the best is to use awatch
on the pinia stateThe
handler
would write to local storage.It's also at the handler level that one should check for specific stores' state as they are created on the fly as they get used. This means that when the watcher is created, it's observing an empty object (
pinia.state.value
). You could still watch a specific store with