在做vuex数据存储中,我们面临浏览器刷新页面数据丢失 解决数据丢失,如何持久化需以下几步
一:npm install vuex-persistedstate
二:store/index.js中引入 : vuex-persistedstate
import Vue from 'vue'
import Vuex from 'vuex'
//引入插件
import persistedState from 'vuex-persistedstate'
Vue.use(Vuex)
export default new Vuex.Store({ state: { name:"111111" }, mutations: {}, actions: {}, modules: {}, //默认存储到localStorage plugins: [
//默认localStorage
persistedState()
//下面这个设置为sessionStorage
//persistedState({ storage: window.sessionStorage })
//设置cookie
//persistedState({
// storage: {
// getItem: key => Cookies.get(key),
// setItem: (key, value) => Cookies.set(key, value, { expires: 7 }),
// removeItem: key => Cookies.remove(key)
// }
//})
] })