|
16 | 16 | </template>
|
17 | 17 |
|
18 | 18 | <script setup>
|
19 |
| -import { computed, watch } from 'vue' |
| 19 | +import settings from "@/settings" |
| 20 | +import { watch } from 'vue' |
20 | 21 | import { storeToRefs } from 'pinia/dist/pinia'
|
21 | 22 | import { useRoute } from 'vue-router'
|
22 |
| -
|
23 | 23 | import { useBasicStore } from '@/store/basic'
|
24 |
| -import { cloneDeep } from '@/hooks/use-common' |
25 |
| -const { settings, cachedViews } = storeToRefs(useBasicStore()) |
| 24 | +const { cachedViews } = storeToRefs(useBasicStore()) |
26 | 25 | const route = useRoute()
|
27 |
| -const key = computed(() => route.path) |
28 |
| -/*listen the component name changing, then to keep-alive the page*/ |
29 |
| -// cachePage: is true, keep-alive this Page |
30 |
| -// leaveRmCachePage: is true, keep-alive remote when page leave |
31 | 26 | let oldRoute = {}
|
32 | 27 | let cacheGroup = []
|
33 |
| -let deepOldRouter = null |
34 | 28 | const basicStore = useBasicStore()
|
35 |
| -const removeDeepChildren = (deepOldRouter) => { |
36 |
| - deepOldRouter.children?.forEach((fItem) => { |
37 |
| - basicStore.delCacheViewDeep(fItem.name) |
38 |
| - }) |
39 |
| -} |
| 29 | +
|
40 | 30 | watch(
|
41 |
| - () => route.name, |
42 |
| - () => { |
43 |
| - const routerLevel = route.matched.length |
44 |
| - //缓存组处理 |
45 |
| - //first judge cacheGroup and then remove |
46 |
| - if (cacheGroup.length) { |
47 |
| - if (!cacheGroup.includes(route.name)) { |
48 |
| - cacheGroup.forEach((item) => { |
49 |
| - basicStore.delCachedView(item) |
| 31 | + () => route.name, |
| 32 | + () => { |
| 33 | + //缓存组处理 |
| 34 | + //first judge cacheGroup and then remove |
| 35 | + if (cacheGroup.length) { |
| 36 | + if (!cacheGroup.includes(route.name)) { |
| 37 | + cacheGroup.forEach((item) => { |
| 38 | + basicStore.delCachedView(item) |
| 39 | + }) |
| 40 | + } |
| 41 | + } |
| 42 | + //and then cache the current router config page |
| 43 | + if (route.meta?.cacheGroup) { |
| 44 | + cacheGroup = route.meta?.cacheGroup || [] |
| 45 | + cacheGroup.forEach((fItem) => { |
| 46 | + basicStore.addCachedView(fItem) |
50 | 47 | })
|
51 | 48 | }
|
52 |
| - } |
53 |
| - //and then cache the current router config page |
54 |
| - if (route.meta?.cacheGroup) { |
55 |
| - cacheGroup = route.meta?.cacheGroup || [] |
56 |
| - cacheGroup.forEach((fItem) => { |
57 |
| - basicStore.addCachedView(fItem) |
58 |
| - }) |
59 |
| - } |
60 |
| -
|
61 |
| - //二级路由处理 |
62 |
| - if (routerLevel === 2) { |
63 |
| - if (deepOldRouter?.name) { |
64 |
| - if (deepOldRouter.meta?.leaveRmCachePage && deepOldRouter.meta?.cachePage) { |
65 |
| - basicStore.delCachedView(deepOldRouter.name) |
66 |
| - //remove the deepOldRouter‘s children component |
67 |
| - removeDeepChildren(deepOldRouter) |
68 |
| - } |
69 |
| - } else { |
70 |
| - if (oldRoute?.name) { |
71 |
| - if (oldRoute.meta?.leaveRmCachePage && oldRoute.meta?.cachePage) { |
72 |
| - basicStore.delCachedView(oldRoute.name) |
73 |
| - } |
| 49 | + //二级路由处理 |
| 50 | + if (oldRoute?.name) { |
| 51 | + if (oldRoute.meta?.leaveRmCachePage && oldRoute.meta?.cachePage) { |
| 52 | + basicStore.delCachedView(oldRoute.name) |
74 | 53 | }
|
75 | 54 | }
|
76 |
| -
|
77 | 55 | if (route.name) {
|
78 | 56 | if (route.meta?.cachePage) {
|
79 | 57 | basicStore.addCachedView(route.name)
|
80 | 58 | }
|
81 | 59 | }
|
82 |
| - deepOldRouter = null |
83 |
| - } |
84 |
| -
|
85 |
| - //三级路由处理 |
86 |
| - if (routerLevel === 3) { |
87 |
| - //三级时存储当前路由对象的上一级 |
88 |
| - const parentRoute = route.matched[1] |
89 |
| - //deepOldRouter不为空,且deepOldRouter不是当前路由的父对象,则需要清除deepOldRouter缓存 |
90 |
| - //一般为三级路由跳转三级路由的情况 |
91 |
| - if (deepOldRouter?.name && deepOldRouter.name !== parentRoute.name) { |
92 |
| - if (deepOldRouter.meta?.leaveRmCachePage && deepOldRouter.meta?.cachePage) { |
93 |
| - basicStore.delCachedView(deepOldRouter.name) |
94 |
| - //remove the deepOldRouter‘s children component |
95 |
| - removeDeepChildren(deepOldRouter) |
96 |
| - } |
97 |
| - } else { |
98 |
| - //否则走正常两级路由处理流程 |
99 |
| - if (oldRoute?.name) { |
100 |
| - if (oldRoute.meta?.leaveRmCachePage && oldRoute.meta?.cachePage) { |
101 |
| - basicStore.delCacheViewDeep(oldRoute.name) |
102 |
| - } |
103 |
| - } |
104 |
| - } |
105 |
| - //取的是第二级的name |
106 |
| - if (parentRoute.name && parentRoute.meta?.cachePage) { |
107 |
| - deepOldRouter = parentRoute |
108 |
| - basicStore.addCachedView(deepOldRouter.name) |
109 |
| - if (route.name) { |
110 |
| - if (route.meta?.cachePage) { |
111 |
| - //和第三级的name进行缓存 |
112 |
| - basicStore.addCachedViewDeep(route.name) |
113 |
| - } |
114 |
| - } |
115 |
| - } |
116 |
| - } |
117 |
| - oldRoute = cloneDeep({ name: route.name, meta: route.meta }) |
118 |
| - }, |
119 |
| - { immediate: true } |
| 60 | + oldRoute = JSON.parse(JSON.stringify({ name: route.name, meta: route.meta })) |
| 61 | + }, |
| 62 | + { immediate: true } |
120 | 63 | )
|
121 | 64 | </script>
|
122 | 65 |
|
|
0 commit comments