vuex页面刷新数据丢失的解决办法

app.vue

import store from './store'

  mounted () {
      // 解决vuex刷新丢失
      window.addEventListener("beforeunload",()=>{ 
      localStorage.setItem("userComMsg",JSON.stringify(store.state))
    });
    //在页面加载时读取localStorage里的状态信息
    if(localStorage.getItem("userComMsg")){ 
      Object.assign(store.state,JSON.parse(localStorage.getItem("userComMsg")));
      //使用后清除内存
      setTimeout(function () { 
        localStorage.removeItem("userComMsg");
      },300)
    }


  }

引用

你可能感兴趣的:(vue.js)