vue 防止页面刷新vuex数据丢失

app.vue 

created() {
          if(localStorage.getItem("store")){
            this.$store.replaceState(
                Object.assign({},this.$store.state,JSON.parse(window.localStorage.getItem("store")))
            );
          }
          //在页面刷新时将vuex里的信息保存到localStorage里
          window.addEventListener("beforeunload",()=>{
            window.localStorage.setItem("store",JSON.stringify(this.$store.state));
          });
        },
posted @ 2021-04-21 17:38  起风了1573  阅读(288)  评论(0)    收藏  举报