vuex-persistedstate

vuex-persistedstate

安装

1
yarn add vuex-persistedstate

使用

vuex-persistedstate 3.x (for Vuex 3 and Vue 2)

1
2
3
4
5
6
7
import Vuex from "vuex";
import createPersistedState from "vuex-persistedstate";

const store = new Vuex.Store({
  // ...
  plugins: [createPersistedState()],
});

vuex-persistedstate 4.x (for Vuex 4 and Vue 3)

1
2
3
4
5
6
7
import { createStore } from "vuex";
import createPersistedState from "vuex-persistedstate";

const store = createStore({
  // ...
  plugins: [createPersistedState()],
});

新的插件实例可以在单独的文件中创建,但必须导入并添加到主Vuex文件中的plugins对象中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* 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]
})
1
createPersistedState({ storage: window.sessionStorage });

createPersistedState([options])

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
key <String>: The key to store the persisted state under. Defaults to vuex.

paths <Array>: An array of any paths to partially persist the state. If no paths are given, the complete state is persisted. If an empty array is given, no state is persisted. Paths must be specified using dot notation. If using modules, include the module name. eg: "auth.user" Defaults to undefined.

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 to store => handler => store.subscribe(handler).

storage <Object>: Instead of (or in combination with) getState and setState. Defaults to localStorage.

getState <Function>: A function that will be called to rehydrate a previously persisted state. Defaults to using storage.

setState <Function>: A function that will be called to persist the given state. Defaults to using storage.

filter <Function>: A function that will be called to filter any mutations which will trigger setState on storage eventually. Defaults to () => true.

overwrite <Boolean>: When rehydrating, whether to overwrite the existing state with the output from getState directly, instead of merging the two objects with deepmerge. Defaults to false.

arrayMerger <Function>: A function for merging arrays when rehydrating state. Defaults to function (store, saved) { return saved } (saved state replaces supplied state).

rehydrated <Function>: A function that will be called when the rehydration is finished. Useful when you are using Nuxt.js, which the rehydration of the persisted state happens asynchronously. Defaults to store => {}

fetchBeforeUse <Boolean>: A boolean indicating if the state should be fetched from storage before the plugin is used. Defaults to false.

assertStorage <Function>: An overridable function to ensure storage is available, fired on plugins's initialization. Default one is performing a Write-Delete operation on the given Storage instance. Note, default behaviour could throw an error (like DOMException: QuotaExceededError).