useFrameKeepAlive.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import type { AppRouteRecordRaw } from '/@/router/types';
  2. import { computed, toRaw, unref } from 'vue';
  3. import { useMultipleTabStore } from '/@/store/modules/multipleTab';
  4. import { uniqBy } from 'lodash-es';
  5. import { useMultipleTabSetting } from '/@/hooks/setting/useMultipleTabSetting';
  6. import { useRouter } from 'vue-router';
  7. export function useFrameKeepAlive() {
  8. const router = useRouter();
  9. const { currentRoute } = router;
  10. const { getShowMultipleTab } = useMultipleTabSetting();
  11. const tabStore = useMultipleTabStore();
  12. const getFramePages = computed(() => {
  13. const ret = getAllFramePages(toRaw(router.getRoutes()) as unknown as AppRouteRecordRaw[]) || [];
  14. return ret;
  15. });
  16. const getOpenTabList = computed((): string[] => {
  17. return tabStore.getTabList.reduce((prev: string[], next) => {
  18. if (next.meta && Reflect.has(next.meta, 'frameSrc')) {
  19. prev.push(next.name as string);
  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 = uniqBy(res, 'name');
  36. return res;
  37. }
  38. function showIframe(item: AppRouteRecordRaw) {
  39. return item.name === unref(currentRoute).name;
  40. }
  41. function hasRenderFrame(name: string) {
  42. if (!unref(getShowMultipleTab)) {
  43. return unref(router.currentRoute).name === name;
  44. }
  45. if (!unref(getOpenTabList)) {
  46. return unref(getOpenTabList).includes(name);
  47. }
  48. if (unref(router.currentRoute).meta.frameSrc) {
  49. return true;
  50. }
  51. }
  52. return { hasRenderFrame, getFramePages, showIframe, getAllFramePages };
  53. }