vue cli3中 vueRouter刷新时返回首页,刷新时store中数据丢失

使用vue-cli3.0框架搭建后台管理系统,使用了store模式,因为每次手动F5刷新时,store中的数据会自动清除,导致每次刷新就丢失所有数据,
所以解决办法就是添加一个监听事件,在页面丢失前存储store中的所有数据,在重新加载后再取出
		于是在app.vue中添加事件监听,具体代码如下:
created(){
			//在页面加载时读取sessionStorage里的状态信息
			if (sessionStorage.getItem("store") ) {
				this.$store.replaceState(Object.assign({}, this.$store.state,JSON.parse(sessionStorage.getItem("store"))))
			}
			//在页面刷新时将vuex里的信息保存到sessionStorage里
			window.addEventListener("beforeunload",()=>{
				sessionStorage.setItem("store",JSON.stringify(this.$store.state))
			})
    },

于是,就解决了刷新产生的数据丢失问题,拉拉啦(≧▽≦)/

猜你喜欢

转载自blog.csdn.net/Taurus_0811/article/details/88620999