vuex刷新丢失解决方案

App.vue


   mounted() {
      /*全局监听,页面刷新的时候将store里state的值存到sessionStorage中,然后从sessionStorage中获取,再赋值给store。
然后再把session里面存的删除即可,相当于中间件的作用。*/
      //在页面加载时读取sessionStorage里的状态信息
      if (sessionStorage.getItem("store")) {
         this.$store.replaceState(Object.assign({}, this.$store.state, JSON.parse(sessionStorage.getItem("store"))));
         sessionStorage.removeItem("store");
      }

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

版权声明:本文为weixin_45432615原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。