index.tsx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import type { FunctionalComponent } from 'vue';
  2. import { computed, defineComponent, unref, Transition, KeepAlive } from 'vue';
  3. import { RouterView, RouteLocation } from 'vue-router';
  4. import FrameLayout from '/@/layouts/iframe/index.vue';
  5. import { useRootSetting } from '/@/hooks/setting/useRootSetting';
  6. import { useTransitionSetting } from '/@/hooks/setting/useTransitionSetting';
  7. import { useCache } from './useCache';
  8. import { useMultipleTabSetting } from '/@/hooks/setting/useMultipleTabSetting';
  9. // import { createAsyncComponent } from '/@/utils/factory/createAsyncComponent';
  10. interface DefaultContext {
  11. Component: FunctionalComponent & { type: Indexable };
  12. route: RouteLocation;
  13. }
  14. // const FrameLayout=createAsyncComponent(()=>'/@/layouts/iframe/index.vue')
  15. export default defineComponent({
  16. name: 'PageLayout',
  17. setup() {
  18. const { getCaches } = useCache(true);
  19. const { getShowMultipleTab } = useMultipleTabSetting();
  20. const { getOpenKeepAlive, getCanEmbedIFramePage } = useRootSetting();
  21. const { getBasicTransition, getEnableTransition } = useTransitionSetting();
  22. const openCache = computed(() => unref(getOpenKeepAlive) && unref(getShowMultipleTab));
  23. return () => {
  24. return (
  25. <div>
  26. <RouterView>
  27. {{
  28. default: ({ Component, route }: DefaultContext) => {
  29. // No longer show animations that are already in the tab
  30. const cacheTabs = unref(getCaches);
  31. const isInCache = cacheTabs.includes(route.name as string);
  32. const name =
  33. isInCache && route.meta.loaded && unref(getEnableTransition)
  34. ? 'fade-slide'
  35. : null;
  36. // When the child element is the parentView, adding the key will cause the component to be executed multiple times. When it is not parentView, you need to add a key, because it needs to be compatible with the same route carrying different parameters
  37. const isParentView = Component?.type.parentView;
  38. const componentKey = isParentView ? {} : { key: route.fullPath };
  39. const renderComp = () => <Component {...componentKey} />;
  40. const PageContent = unref(openCache) ? (
  41. <KeepAlive include={cacheTabs}>{renderComp()}</KeepAlive>
  42. ) : (
  43. renderComp()
  44. );
  45. if (!unref(getEnableTransition)) {
  46. return PageContent;
  47. }
  48. return (
  49. <Transition
  50. name={name || route.meta.transitionName || unref(getBasicTransition)}
  51. mode="out-in"
  52. appear={true}
  53. >
  54. {() => PageContent}
  55. </Transition>
  56. );
  57. },
  58. }}
  59. </RouterView>
  60. {unref(getCanEmbedIFramePage) && <FrameLayout />}
  61. </div>
  62. );
  63. };
  64. },
  65. });