vuex-ls
v1.1.1
Published
Persist Vuex state with localStorage.
Downloads
1
Readme
vuex-persistedstate
Persist Vuex state with localStorage.
Installation
$ npm install vuex-persistedstate
Usage
import createPersistedState from 'vuex-persistedstate'
const store = new Vuex.Store({
// ...
plugins: [createPersistedState()]
})
API
createPersistedState([options])
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: [])getState <Function>
: A function that will be called to rehydrate a previously persisted state. Defaults to localStorage.setState <Function>
: A function that will be called to persist the given state. Defaults to localStorage.reducer <Function>
: A function that will be called to reduce the state to persist based on the given paths. Defaults to include the values.
Customization
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({
getState: (key) => Cookies.getJSON(key),
setState: (key, state) => Cookies.set(key, state, { expires: 3, secure: true })
})
]
})