robinvdvleuten /vuex-persistedstate

💾 Persist and rehydrate your Vuex state between page reloads.

★ 5,658⑂ 368👁 1⑃ 172● 268▤ 31
JavaScriptMITCreated 2016-09-25npm.im/vuex-persistedstate
下载:v4.1.0vuex-persistedstate-4.1.0 ↓4.1.02021-09-21
+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

Latest release

v4.1.0 v4.1.0 2021-09-21
Minor Changes 

- Prevent JSON.parse to be called with a non string value: 400

 Credits 

Huge thanks to @joaoaraujo-hotmart for helping!

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON