vben
2020-12-13 27e50b47479af8eaeb4be020aeb0fcbdb4308295
提交 | 用户 | age
c303ec 1 import { computed, ref, unref } from 'vue';
V 2 import { useRootSetting } from '/@/hooks/setting/useRootSetting';
3 import { tryTsxEmit } from '/@/utils/helper/vueHelper';
4 import { tabStore, PAGE_LAYOUT_KEY } from '/@/store/modules/tab';
5
6 import { useRouter } from 'vue-router';
7
8 const ParentLayoutName = 'ParentLayout';
9 export function useCache(isPage: boolean) {
10   const name = ref('');
11   const { currentRoute } = useRouter();
12
f4621c 13   tryTsxEmit((instance) => {
V 14     const routeName = instance.type.name;
c303ec 15     if (routeName && ![ParentLayoutName].includes(routeName)) {
V 16       name.value = routeName;
17     } else {
18       const matched = currentRoute.value.matched;
19       const len = matched.length;
20       if (len < 2) return;
21       name.value = matched[len - 2].name as string;
22     }
23   });
f4621c 24
c303ec 25   const { getOpenKeepAlive } = useRootSetting();
V 26
27   const getCaches = computed((): string[] => {
28     if (!unref(getOpenKeepAlive)) {
29       return [];
30     }
31     const cached = tabStore.getCachedMapState;
32
33     if (isPage) {
34       //  page Layout
35       return cached.get(PAGE_LAYOUT_KEY) || [];
36     }
37     const cacheSet = new Set<string>();
38     cacheSet.add(unref(name));
39
40     const list = cached.get(unref(name));
b88465 41
c303ec 42     if (!list) {
V 43       return Array.from(cacheSet);
44     }
45     list.forEach((item) => {
46       cacheSet.add(item);
47     });
b88465 48
c303ec 49     return Array.from(cacheSet);
V 50   });
51   return { getCaches };
52 }