This is a known use case. There are different solutions.
For example, one can use vuex-persistedstate
. This is a plugin for vuex
to handle and store state between page refreshes.
Sample code:
import { Store } from 'vuex'
import createPersistedState from 'vuex-persistedstate'
import * as Cookies from 'js-cookie'
const store = new Store({
// ...
plugins: [
createPersistedState({
getState: (key) => Cookies.getJSON(key),
setState: (key, state) => Cookies.set(key, state, { expires: 3, secure: true })
})
]
})
What we do here is simple:
- you need to install
js-cookie
- on
getState
we try to load saved state fromCookies
- on
setState
we save our state toCookies
Docs and installation instructions: https://www.npmjs.com/package/vuex-persistedstate