解决 vue-admin-template 刷新页面 TagsView 丢失问题

在 src / layout / TagsView / index.vue 页面中 methods: 方法中做如下修改

1. 添加代码段

beforeUnload() {
    
    
      // 监听页面刷新
      window.addEventListener("beforeunload", () => {
    
    
        // visitedViews数据结构太复杂无法直接JSON.stringify处理,先转换需要的数据
        let tabViews = this.visitedViews.map(item => {
    
    
          return {
    
    
            fullPath: item.fullPath,
            hash: item.hash,
            meta: {
    
     ...item.meta },
            name: item.name,
            params: {
    
     ...item.params },
            path: item.path,
            query: {
    
     ...item.query },
            title: item.title
          };
        });
        sessionStorage.setItem("tabViews", JSON.stringify(tabViews));
      });
      // 页面初始化加载判断缓存中是否有数据
      let oldViews = JSON.parse(sessionStorage.getItem("tabViews")) || [];
      if (oldViews.length > 0) {
    
    
        this.$store.state.tagsView.visitedViews = oldViews;
      }
    }

在这里插入图片描述

2. 在mounted() 中引用 beforedUnload 函数

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/gtosky4u/article/details/115177292