lin
2024-04-09 af016bea6ede06378d388683f832e4c31dd50922
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
 * @Author: shuishen 1109946754@qq.com
 * @Date: 2023-12-14 17:10:00
 * @LastEditors: shuishen 1109946754@qq.com
 * @LastEditTime: 2024-01-22 13:23:37
 * @FilePath: \jczz_web\src\cache.js
 * @Description:
 *
 * Copyright (c) 2024 by shuishen, All Rights Reserved.
 */
import Vue from "vue";
import store from "./store";
Vue.mixin({
  beforeRouteEnter: function (to, from, next) {
    next(() => {
      let avueView = document.getElementById("avue-view");
      if (avueView && to.meta.savedPosition) {
        avueView.scrollTop = to.meta.savedPosition;
      }
    });
  },
  beforeRouteLeave: function (to, from, next) {
    let avueView = document.getElementById("avue-view");
    if (from && from.meta.keepAlive) {
      if (avueView) {
        from.meta.savedPosition = avueView.scrollTop;
      }
      console.log(store.state.tags.tagList, this.$route.fullPath, 909999);
      const result =
        this.$route.meta.keepAlive === true &&
        store.state.tags.tagList.some((ele) => {
          return ele.value === this.$route.fullPath;
        });
      if (this.$vnode && !result) {
        from.meta.savedPosition = 0;
        if (
          this.$vnode.parent &&
          this.$vnode.parent.componentInstance &&
          this.$vnode.parent.componentInstance.cache
        ) {
          if (this.$vnode.componentOptions) {
            let key =
              this.$vnode.key == null
                ? this.$vnode.componentOptions.Ctor.cid +
                  (this.$vnode.componentOptions.tag
                    ? `::${this.$vnode.componentOptions.tag}`
                    : "")
                : this.$vnode.key;
            let cache = this.$vnode.parent.componentInstance.cache;
            let keys = this.$vnode.parent.componentInstance.keys;
            if (cache[key]) {
              if (keys.length) {
                let index = keys.indexOf(key);
                if (index > -1) {
                  keys.splice(index, 1);
                }
              }
              delete cache[key];
            }
          }
        }
      }
    }
    next();
  },
});