vue-vben-admin/src/layouts/page/index.tsx

81 lines
2.5 KiB
TypeScript
Raw Normal View History

2020-11-23 23:24:13 +08:00
import type { FunctionalComponent } from 'vue';
import { computed, defineComponent, unref, Transition, KeepAlive } from 'vue';
2020-10-30 21:32:05 +08:00
import { RouterView, RouteLocation } from 'vue-router';
2020-09-28 20:19:10 +08:00
2020-10-30 21:32:05 +08:00
import FrameLayout from '/@/layouts/iframe/index.vue';
2020-09-28 20:19:10 +08:00
import { useTransition } from './useTransition';
2020-11-23 23:24:13 +08:00
import { useMenuSetting } from '/@/hooks/setting/useMenuSetting';
import { useRootSetting } from '/@/hooks/setting/useRootSetting';
import { useMultipleTabSetting } from '/@/hooks/setting/useMultipleTabSetting';
2020-09-28 20:19:10 +08:00
import { tabStore } from '/@/store/modules/tab';
2020-11-23 23:24:13 +08:00
interface DefaultContext {
Component: FunctionalComponent;
route: RouteLocation;
}
2020-09-28 20:19:10 +08:00
export default defineComponent({
name: 'PageLayout',
setup() {
2020-11-23 23:24:13 +08:00
const { getShow } = useMenuSetting();
const {
getOpenKeepAlive,
getRouterTransition,
getOpenRouterTransition,
getCanEmbedIFramePage,
} = useRootSetting();
2020-11-12 23:58:44 +08:00
2020-11-23 23:24:13 +08:00
const { getMax } = useMultipleTabSetting();
2020-09-28 20:19:10 +08:00
2020-11-23 23:24:13 +08:00
const transitionEvent = useTransition();
const openCacheRef = computed(() => unref(getOpenKeepAlive) && unref(getShow));
2020-09-28 20:19:10 +08:00
2020-11-23 23:24:13 +08:00
const getCacheTabsRef = computed(() => tabStore.getKeepAliveTabsState as string[]);
return () => {
return (
<div>
<RouterView>
{{
2020-11-23 23:24:13 +08:00
default: ({ Component, route }: DefaultContext) => {
2020-11-02 23:04:25 +08:00
// No longer show animations that are already in the tab
2020-11-12 23:58:44 +08:00
const cacheTabs = unref(getCacheTabsRef);
const isInCache = cacheTabs.includes(route.name as string);
const name = isInCache && route.meta.inTab ? 'fade' : null;
2020-11-02 23:04:25 +08:00
2020-11-23 23:24:13 +08:00
const renderComp = () => <Component key={route.fullPath} />;
const PageContent = unref(openCacheRef) ? (
<KeepAlive max={unref(getMax)} include={cacheTabs}>
{renderComp()}
</KeepAlive>
) : (
2020-11-23 23:24:13 +08:00
renderComp()
);
2020-11-23 23:24:13 +08:00
return unref(getOpenRouterTransition) ? (
<Transition
2020-11-23 23:24:13 +08:00
{...transitionEvent}
name={name || route.meta.transitionName || unref(getRouterTransition)}
mode="out-in"
2020-10-18 21:55:21 +08:00
appear={true}
>
2020-11-23 23:24:13 +08:00
{() => PageContent}
</Transition>
) : (
2020-11-23 23:24:13 +08:00
PageContent
);
},
}}
</RouterView>
2020-11-23 23:24:13 +08:00
{unref(getCanEmbedIFramePage) && <FrameLayout />}
</div>
);
2020-09-28 20:19:10 +08:00
};
},
});