使用vuex-persistedstate持久化vuex状态

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

推荐阅读更多精彩内容