robinvdvleuten /vuex-persistedstate
💾 Persist and rehydrate your Vuex state between page reloads.
★ 5,658⑂ 368👁 1⑃ 172● 268▤ 31
+0
日增长
5,658
stars
368
forks
1
watchers
172
prs
268
issues
31
releases
README
vuex-persistedstate
Persist and rehydrate your Vuex state between page reloads.
🚨 Not maintained anymore! As I don't use Vue in my day to day work, it becomes very hard to stay up to date with any changes with things like Vuex, Nuxt.js and other tools used by the community. That's why I decided to stop spending my spare time to this repository. Feel free to reach out if you would like to take over ownership of the package on NPM. Thank you for any contribution any of you had made to this project 🙏.
Install
bash
npm install --save vuex-persistedstate
The UMD build is also available on unpkg:
html
You can find the library on window.createPersistedState.
Usage
js
import { createStore } from "vuex";
import createPersistedState from "vuex-persistedstate";
const store = createStore({
// ...
plugins: [createPersistedState()],
});
For usage with for Vuex 3 and Vue 2, please see 3.x.x branch.
Examples
Check out a basic example on CodeSandbox.
Or configured to use with js-cookie.
Or configured to use with secure-ls
Example with Vuex modules
New plugin instances can be created in separate files, but must be imported and added to plugins object in the main Vuex file.
js
/ module.js /
export const dataStore = {
state: {
data: []
}
}
/ store.js /
import { dataStore } from './module'
const dataState = createPersistedState({
paths: ['data']
})
export new Vuex.Store({
modules: {
dataStore
},
plugins: [dataState]
})
Example with Nuxt.js
It is possible to use vuex-persistedstate with Nuxt.js. It must be included as a NuxtJS plugin:
With local storage (client-side only)
javascript
// nuxt.config.js
...
/
Naming your plugin 'xxx.client.js' will make it execute only on the client-side.
https://nuxtjs.org/guide/plugins/name-conventional-plugin
/
plugins: [{ src: '~/plugins/persistedState.client.js' }]
...
javascript
// ~/plugins/persistedState.client.js
import createPersistedState from 'vuex-persistedstate'
export default ({store}) = {
createPersistedState({
key: 'yourkey',
paths: [...]
...
})(store)
}
Using cookies (universal client + server-side)
Add cookie and js-cookie:
npm install --save cookie js-cookie
or yarn add cookie js-cookie
javascript
// nuxt.config.js
...
plugins: [{ src: '~/plugins/persistedState.js'}]
...
javascript
// ~/plugins/persistedState.js
import createPersistedState from 'vuex-persistedstate';
import as Cookies from 'js-cookie';
import cookie from 'cookie';
export default ({ store, req }) = {
createPersistedState({
paths: [...],
storage: {
getItem: (key) = {
// See https://nuxtjs.org/guide/plugins/using-process-flags
if (process.server) {
const parsedCookies = cookie.parse(req.headers.cookie);
return parsedCookies[key];
} else {
return Cookies.get(key);
}
}Top issues
#240the bug of filter12 comments · 2020-01-26#288[Feature Request] Add `ignorePaths` or `excludePaths` option9 comments · 2020-07-15#398argument str must be a string on nuxt server persist7 comments · 2021-02-28#135v3.0 preview5 comments · 2018-04-15#388missing data in some modules4 comments · 2021-01-22#376Is it possible to use with Streamsaver.js to avoid size limits of localstorage?4 comments · 2020-12-08#440Clearing localstorage in developer tools not working2 comments · 2021-12-16#436vuex-persistedstate does not follow vuex plugin guidelines2 comments · 2021-12-07
Latest release
v4.1.0 v4.1.0 2021-09-21Minor Changes - Prevent JSON.parse to be called with a non string value: 400 Credits Huge thanks to @joaoaraujo-hotmart for helping!