借助插件来解决vuex页面刷新后数据丢失的问题。
安装插件
npm install vuex-persistedstate --save
配置使用(默认存储到localStorage)
import createPersistedState from "vuex-persistedstate" const dataInfo = { state:{}, mutations:{}, actions: {}, getters:{}, plugins: [createPersistedState()] } export default dataInfo
存储到sessionStorage
import createPersistedState from "vuex-persistedstate" const dataInfo = { plugins: [createPersistedState({ state:{}, mutations:{}, actions: {}, getters:{}, storage:window.sessionStorage })] } export default dataInfo
自定义存储
plugins: [ createPersistedState({ storage: window.sessionStorage }) ]