刷新页面后空白,vuex数据丢失

342 阅读1分钟
VueX浏览器刷新数据丢失问题,在APP.vue中添加一下代码就解决喽!


 created() {
    // 解决VueX浏览器刷新数据丢失问题
    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));
    });
  },