useCache.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { computed, ref, unref } from 'vue';
  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. import { useRouter } from 'vue-router';
  6. const ParentLayoutName = 'ParentLayout';
  7. export function useCache(isPage: boolean) {
  8. const name = ref('');
  9. const { currentRoute } = useRouter();
  10. tryTsxEmit((instance: any) => {
  11. const routeName = instance.ctx.$options.name;
  12. if (routeName && ![ParentLayoutName].includes(routeName)) {
  13. name.value = routeName;
  14. } else {
  15. const matched = currentRoute.value.matched;
  16. const len = matched.length;
  17. if (len < 2) return;
  18. name.value = matched[len - 2].name as string;
  19. }
  20. });
  21. const { getOpenKeepAlive } = useRootSetting();
  22. const getCaches = computed((): string[] => {
  23. if (!unref(getOpenKeepAlive)) {
  24. return [];
  25. }
  26. const cached = tabStore.getCachedMapState;
  27. if (isPage) {
  28. // page Layout
  29. // not parent layout
  30. return cached.get(PAGE_LAYOUT_KEY) || [];
  31. }
  32. const cacheSet = new Set<string>();
  33. cacheSet.add(unref(name));
  34. const list = cached.get(unref(name));
  35. if (!list) {
  36. return Array.from(cacheSet);
  37. }
  38. list.forEach((item) => {
  39. cacheSet.add(item);
  40. });
  41. return Array.from(cacheSet);
  42. });
  43. return { getCaches };
  44. }