vuex刷新页面数据丢失(二)

上来就干货!

import Vue from 'vue/dist/vue.common.js'  //有的是 import Vue from 'vue'  参照main.js引入vue的方式
import Vuex from 'vuex'

import state from './state'
import getters from './getters'
import actions from './actions'
import mutations from './mutations'

Vue.use(Vuex)

const store = {
  state,
  getters,
  actions,
  mutations
}


// 防止刷新vuex数据丢失
window.addEventListener('beforeunload', (e) => {
  // 监听离开当前页面
  window.sessionStorage.setItem('state', JSON.stringify(store.state))
});
const sesionState = window.sessionStorage.getItem('state');
if (sesionState) {
  store.state = JSON.parse(sesionState);
  window.sessionStorage.removeItem('state');
}

export default new Vuex.Store(store)

你可能感兴趣的:(vuex刷新页面数据丢失(二))