使用vuex-persistedstate持久化vuex状态

2019-11-06  本文已影响0人  _undefined

vuex-persistedstate

Persist and rehydrate your Vuex state between page reloads.

就是同步vuex状态到本地存储localStoragesessionStorage或者cookie

安装

npm install --save vuex-persistedstate

or

<script src="https://unpkg.com/vuex-persistedstate/dist/vuex-persistedstate.umd.js"></script>

使用

import createPersistedState from "vuex-persistedstate";

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

默认不设置storage使用localStorage

uni-app中使用

const store = new Vuex.Store({
    // ...
    plugins: [
        CreatePersistedState({
            storage: {
                getItem: key => uni.getStorageSync(key),
                setItem: (key, value) => uni.setStorageSync(key, value),
                removeItem: key => uni.removeStorageSync(key)
            }
        })
    ]
    // ...
});

文档

vuex-persistedstate

上一篇下一篇

猜你喜欢

热点阅读