1. vuex 全局数据
  2. window.addEventListener("beforeunload",()=>{ }) 监听页面刷新
  3. 使用localStorage 临时存储数据

思路:在页面刷新的时候,将vuex中的数据存储到localstorage ,然后刷新结束后再将localstorage里的数赋值给store并清楚localstorage

代码

//js 监听页面alert刷新
    window.addEventListener("beforeunload", () => {
      localStorage.setItem("stateInfo", JSON.stringify(this.$store.state));
    });
    if (localStorage.getItem("stateInfo")) {
      this.$store.replaceState(
        Object.assign(
          {},
          this.$store.state,
          JSON.parse(localStorage.getItem("stateInfo"))
        )
      );
    }

    localStorage.removeItem("stateInfo");
    console.log("名字", this.$store.state);

还可以考虑使用 vuex-persistedstate 这个插件,具体使用可以上npm官网搜索了解。  

相关文章:

  • 2022-02-14
  • 2021-08-01
  • 2022-12-23
  • 2022-12-23
  • 2021-07-14
  • 2021-07-30
  • 2022-12-23
猜你喜欢
  • 2019-01-30
  • 2022-12-23
  • 2021-09-11
  • 2021-09-22
  • 2021-06-16
  • 2022-12-23
相关资源
相似解决方案