我用的方法很简单,在App.vue中的生命周期函数created中,监听页面刷新,在页面刷新的时候,将store的数据及时存储在sessionStorage中,然后刷新完毕后就可以将sessionStorage中的数据再取出来用
created () { //页面刷新store数据备份
if (localStorage.cmstoken) {
this.$router.replace("/data");
}else {
this.$router.replace("/");
}
if (sessionStorage.getItem("store") ) { //在页面加载时读取sessionStorage里的状态信息
this.$store.replaceState(Object.assign({}, this.$store.state,JSON.parse(sessionStorage.getItem("store"))))
}
window.addEventListener("beforeunload",()=>{ //在页面刷新时将vuex里的信息保存到sessionStorage里
sessionStorage.setItem("store",JSON.stringify(this.$store.state))
})
},