关于前端:vuex在F5时数据丢失的两种办法

1.APP.vue

export default ({
  created() {
    //在页面加载时读取sessionStorage里的状态信息
    if (sessionStorage.getItem("store")) {
      this.$store.replaceState(
        Object.assign(
          {},
          this.$store.state,
          JSON.parse(sessionStorage.getItem("store"))
        )
      );
    }
    //在页面刷新时将vuex里的信息保留到sessionStorage里
    window.addEventListener("beforeunload", () => {
      // sessionStorage.setItem("store", "");
      sessionStorage.setItem("store", JSON.stringify(this.$store.state));
    });
  }
})
</script>

2

`npm install vuex-persistedstate --save`    

const store = new Vuex.Store({
  
  plugins: [createPersistedState()] 
})

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理