Persist and rehydrate your Vuex state between page reloads.
$ npm install vuex-persistedstate
import createPersistedState from 'vuex-persistedstate'
const store = new Vuex.Store({
// ...
plugins: [createPersistedState()],
})
It is possible to use vuex-persistedstate with Nuxt.js. Due to the order code is loaded in, vuex-persistedstate must be included as a NuxtJS plugin:
// nuxt.config.js
...
plugins: [{ src: '~/plugins/localStorage.js', ssr: false }]
...
// ~/plugins/localStorage.js
import createPersistedState from 'vuex-persistedstate'
export default ({store}) => {
createPersistedState({
key: 'yourkey',
paths: [...]
...
})(store)
}
Creates a new instance of the plugin with the given options. The following options can be provided to configure the plugin for your specific needs:
-
key <String>
: The key to store the persisted state under. (default: vuex) -
paths <Array>
: An array of any paths to partially persist the state. If no paths are given, the complete state is persisted. (default: []) -
reducer <Function>
: A function that will be called to reduce the state to persist based on the given paths. Defaults to include the values. -
subscriber <Function>
: A function called to setup mutation subscription. Defaults tostore => handler => store.subscribe(handler)
-
storage <Object>
: Instead for (or in combination with)getState
andsetState
. Defaults to localStorage. -
getState <Function>
: A function that will be called to rehydrate a previously persisted state. Defaults to usingstorage
. -
setState <Function>
: A function that will be called to persist the given state. Defaults to usingstorage
. -
filter <Function>
: A function that will be called to filter any mutations which will triggersetState
on storage eventually. Defaults to() => true
-
arrayMerger <Function>
: A function for merging arrays when rehydrating state. Defaults tofunction (store, saved) { return saved }
(saved state replaces supplied state). -
expireTime <Number>
: 单位毫秒,本地储存的延迟过期时间,每次mutation之后都会刷新这个时间,会根据这个时间,在每次页面重载时,选择是否删除已有的本地储存 例:1000,表示10秒钟后无操作,下次刷新页面,会删除本地储存的数据 -
cancelAll <Boolean>
: 默认false, 是否删除所有的本地储存 -
exceptItems <Array<String>>
: [ ], 是否排除所列出的字段,指的是一级字段,例:exceptItems: ['startTime', 'endTime']
If it's not ideal to have the state of the Vuex store inside localstorage. One can easily implement the functionality to use cookies for that (or any other you can think of);
import { Store } from 'vuex'
import createPersistedState from 'vuex-persistedstate'
import * as Cookies from 'js-cookie'
const store = new Store({
// ...
plugins: [
createPersistedState({
storage: {
getItem: key => Cookies.get(key),
// Please see https://github.com/js-cookie/js-cookie#json, on how to handle JSON.
setItem: (key, value) =>
Cookies.set(key, value, { expires: 3, secure: true }),
removeItem: key => Cookies.remove(key),
},
}),
],
})
In fact, any object following the Storage protocol (getItem, setItem, removeItem, etc) could be passed:
createPersistedState({ storage: window.sessionStorage })
This is especially useful when you are using this plugin in combination with server-side rendering, where one could pass an instance of dom-storage.
As it maybe seems at first sight, it's not possible to pass a LocalForage instance as storage
property. This is due the fact that all getters and setters must be synchronous and LocalForage's methods are asynchronous.