useFrameKeepAlive.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import type { AppRouteRecordRaw } from '/@/router/types';
  2. import { computed, toRaw, unref } from 'vue';
  3. import { useRouter } from 'vue-router';
  4. import router from '/@/router';
  5. import { tabStore } from '/@/store/modules/tab';
  6. import { unique } from '/@/utils';
  7. import { useMultipleTabSetting } from '/@/hooks/setting/useMultipleTabSetting';
  8. export function useFrameKeepAlive() {
  9. const { currentRoute } = useRouter();
  10. const { getShowMultipleTab } = useMultipleTabSetting();
  11. const getFramePages = computed(() => {
  12. const ret =
  13. getAllFramePages((toRaw(router.getRoutes()) as unknown) as AppRouteRecordRaw[]) || [];
  14. return ret;
  15. });
  16. const getOpenTabList = computed((): string[] => {
  17. return tabStore.getTabsState.reduce((prev: string[], next) => {
  18. if (next.meta && Reflect.has(next.meta, 'frameSrc')) {
  19. prev.push(next.path!);
  20. }
  21. return prev;
  22. }, []);
  23. });
  24. function getAllFramePages(routes: AppRouteRecordRaw[]): AppRouteRecordRaw[] {
  25. let res: AppRouteRecordRaw[] = [];
  26. for (const route of routes) {
  27. const { meta: { frameSrc } = {}, children } = route;
  28. if (frameSrc) {
  29. res.push(route);
  30. }
  31. if (children && children.length) {
  32. res.push(...getAllFramePages(children));
  33. }
  34. }
  35. res = unique(res, 'name');
  36. return res;
  37. }
  38. function showIframe(item: AppRouteRecordRaw) {
  39. return item.path === unref(currentRoute).path;
  40. }
  41. function hasRenderFrame(path: string) {
  42. return unref(getShowMultipleTab) ? unref(getOpenTabList).includes(path) : true;
  43. }
  44. return { hasRenderFrame, getFramePages, showIframe, getAllFramePages };
  45. }