vuex-persistedstate
Persist and rehydrate your Vuex state between page reloads.
就是同步vuex状态到本地存储localStorage
、 sessionStorage
或者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)
}
})
]
// ...
});
网友评论