loulijun2021
2022-07-11 e7f22e51057aa90e19afee1628dc68a0d7a6c99e
src/layout/components/TagsView/index.vue
@@ -28,6 +28,7 @@
<script>
import ScrollPane from './ScrollPane'
import path from 'path'
import { getCookie } from '@/utils/auth'
export default {
  components: { ScrollPane },
@@ -64,8 +65,35 @@
  mounted() {
    this.initTags()
    this.addTags()
    this.beforeUnload()
  },
  methods: {
    // 解决 vue-admin-template 刷新页面 TagsView 丢失问题
    beforeUnload() {
      // 监听页面刷新
      window.addEventListener('beforeunload', () => {
        // visitedViews数据结构太复杂无法直接JSON.stringify处理,先转换需要的数据
        const 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))
      })
      // 页面初始化加载判断缓存中是否有数据
      const oldViews = JSON.parse(sessionStorage.getItem('tabViews')) || []
      if (oldViews.length > 0) {
        this.$store.state.tagsView.visitedViews = oldViews
      }
    },
    isActive(route) {
      return route.path === this.$route.path
    },