vuex状态管理,在网页刷新数据被清空的解决方法。

https://blog.csdn.net/chen123789hkb/article/details/90175607

在main.js中写入下面的代码段(亲测有效)

//刷新保存状态
if (sessionStorage.getItem("store")) {
store.replaceState(
Object.assign(
{},
store.state,
JSON.parse(sessionStorage.getItem("store"))
)
);
sessionStorage.removeItem("store")
}

//监听,在页面刷新时将vuex里的信息保存到sessionStorage里
window.addEventListener("beforeunload", () => {
sessionStorage.setItem("store", JSON.stringify(store.state));
});

猜你喜欢

转载自www.cnblogs.com/namehou/p/11826113.html