2024-05-19 21:20:42 +08:00
|
|
|
|
<script lang="ts" setup>
|
2025-01-01 11:39:49 +08:00
|
|
|
|
import type { VNode } from 'vue';
|
2024-07-28 14:29:05 +08:00
|
|
|
|
import type {
|
|
|
|
|
RouteLocationNormalizedLoaded,
|
|
|
|
|
RouteLocationNormalizedLoadedGeneric,
|
|
|
|
|
} from 'vue-router';
|
|
|
|
|
|
2025-04-08 20:27:03 +08:00
|
|
|
|
import { computed } from 'vue';
|
2024-07-28 14:29:05 +08:00
|
|
|
|
import { RouterView } from 'vue-router';
|
2024-06-08 19:49:06 +08:00
|
|
|
|
|
2024-07-23 00:03:59 +08:00
|
|
|
|
import { preferences, usePreferences } from '@vben/preferences';
|
2025-05-18 10:33:02 +08:00
|
|
|
|
import { getTabKey, storeToRefs, useTabbarStore } from '@vben/stores';
|
2024-05-19 21:20:42 +08:00
|
|
|
|
|
|
|
|
|
import { IFrameRouterView } from '../../iframe';
|
|
|
|
|
|
|
|
|
|
defineOptions({ name: 'LayoutContent' });
|
|
|
|
|
|
2024-07-30 21:10:28 +08:00
|
|
|
|
const tabbarStore = useTabbarStore();
|
2024-06-23 23:18:55 +08:00
|
|
|
|
const { keepAlive } = usePreferences();
|
2024-06-23 19:39:44 +08:00
|
|
|
|
|
2024-07-10 00:50:41 +08:00
|
|
|
|
const { getCachedTabs, getExcludeCachedTabs, renderRouteView } =
|
|
|
|
|
storeToRefs(tabbarStore);
|
2024-05-19 21:20:42 +08:00
|
|
|
|
|
2025-04-08 20:27:03 +08:00
|
|
|
|
/**
|
|
|
|
|
* 是否使用动画
|
|
|
|
|
*/
|
|
|
|
|
const getEnabledTransition = computed(() => {
|
|
|
|
|
const { transition } = preferences;
|
|
|
|
|
const transitionName = transition.name;
|
|
|
|
|
return transitionName && transition.enable;
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-19 21:20:42 +08:00
|
|
|
|
// 页面切换动画
|
2024-07-20 08:31:05 +08:00
|
|
|
|
function getTransitionName(_route: RouteLocationNormalizedLoaded) {
|
2024-05-19 21:20:42 +08:00
|
|
|
|
// 如果偏好设置未设置,则不使用动画
|
2024-06-01 23:15:29 +08:00
|
|
|
|
const { tabbar, transition } = preferences;
|
|
|
|
|
const transitionName = transition.name;
|
|
|
|
|
if (!transitionName || !transition.enable) {
|
2024-05-19 21:20:42 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 标签页未启用或者未开启缓存,则使用全局配置动画
|
2024-06-01 23:15:29 +08:00
|
|
|
|
if (!tabbar.enable || !keepAlive) {
|
|
|
|
|
return transitionName;
|
2024-05-19 21:20:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果页面已经加载过,则不使用动画
|
2024-06-23 19:39:44 +08:00
|
|
|
|
// if (route.meta.loaded) {
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
2024-05-19 21:20:42 +08:00
|
|
|
|
// 已经打开且已经加载过的页面不使用动画
|
2024-07-20 08:31:05 +08:00
|
|
|
|
// const inTabs = getCachedTabs.value.includes(route.name as string);
|
2024-06-23 19:39:44 +08:00
|
|
|
|
|
2024-07-20 08:31:05 +08:00
|
|
|
|
// return inTabs && route.meta.loaded ? undefined : transitionName;
|
|
|
|
|
return transitionName;
|
2024-05-19 21:20:42 +08:00
|
|
|
|
}
|
2024-07-28 14:29:05 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 转换组件,自动添加 name
|
|
|
|
|
* @param component
|
|
|
|
|
*/
|
|
|
|
|
function transformComponent(
|
|
|
|
|
component: VNode,
|
|
|
|
|
route: RouteLocationNormalizedLoadedGeneric,
|
|
|
|
|
) {
|
2024-10-05 11:11:30 +08:00
|
|
|
|
// 组件视图未找到,如果有设置后备视图,则返回后备视图,如果没有,则抛出错误
|
|
|
|
|
if (!component) {
|
|
|
|
|
console.error(
|
|
|
|
|
'Component view not found,please check the route configuration',
|
|
|
|
|
);
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-28 14:29:05 +08:00
|
|
|
|
const routeName = route.name as string;
|
|
|
|
|
// 如果组件没有 name,则直接返回
|
|
|
|
|
if (!routeName) {
|
|
|
|
|
return component;
|
|
|
|
|
}
|
2024-10-05 11:11:30 +08:00
|
|
|
|
const componentName = (component?.type as any)?.name;
|
2024-07-28 14:29:05 +08:00
|
|
|
|
|
|
|
|
|
// 已经设置过 name,则直接返回
|
|
|
|
|
if (componentName) {
|
|
|
|
|
return component;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// componentName 与 routeName 一致,则直接返回
|
|
|
|
|
if (componentName === routeName) {
|
|
|
|
|
return component;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置 name
|
|
|
|
|
component.type ||= {};
|
|
|
|
|
(component.type as any).name = routeName;
|
|
|
|
|
|
|
|
|
|
return component;
|
|
|
|
|
}
|
2024-05-19 21:20:42 +08:00
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<template>
|
2024-06-23 19:39:44 +08:00
|
|
|
|
<div class="relative h-full">
|
|
|
|
|
<IFrameRouterView />
|
|
|
|
|
<RouterView v-slot="{ Component, route }">
|
2025-04-08 20:27:03 +08:00
|
|
|
|
<Transition
|
|
|
|
|
v-if="getEnabledTransition"
|
|
|
|
|
:name="getTransitionName(route)"
|
|
|
|
|
appear
|
|
|
|
|
mode="out-in"
|
|
|
|
|
>
|
2024-06-23 19:39:44 +08:00
|
|
|
|
<KeepAlive
|
|
|
|
|
v-if="keepAlive"
|
2024-07-10 00:50:41 +08:00
|
|
|
|
:exclude="getExcludeCachedTabs"
|
|
|
|
|
:include="getCachedTabs"
|
2024-06-23 19:39:44 +08:00
|
|
|
|
>
|
|
|
|
|
<component
|
2024-07-28 14:29:05 +08:00
|
|
|
|
:is="transformComponent(Component, route)"
|
2024-06-23 19:39:44 +08:00
|
|
|
|
v-if="renderRouteView"
|
|
|
|
|
v-show="!route.meta.iframeSrc"
|
2025-05-18 10:33:02 +08:00
|
|
|
|
:key="getTabKey(route)"
|
2024-06-23 19:39:44 +08:00
|
|
|
|
/>
|
|
|
|
|
</KeepAlive>
|
2024-07-05 23:15:46 +08:00
|
|
|
|
<component
|
|
|
|
|
:is="Component"
|
|
|
|
|
v-else-if="renderRouteView"
|
2025-05-18 10:33:02 +08:00
|
|
|
|
:key="getTabKey(route)"
|
2024-07-05 23:15:46 +08:00
|
|
|
|
/>
|
2024-06-23 19:39:44 +08:00
|
|
|
|
</Transition>
|
2025-04-08 20:27:03 +08:00
|
|
|
|
<template v-else>
|
|
|
|
|
<KeepAlive
|
|
|
|
|
v-if="keepAlive"
|
|
|
|
|
:exclude="getExcludeCachedTabs"
|
|
|
|
|
:include="getCachedTabs"
|
|
|
|
|
>
|
|
|
|
|
<component
|
|
|
|
|
:is="transformComponent(Component, route)"
|
|
|
|
|
v-if="renderRouteView"
|
|
|
|
|
v-show="!route.meta.iframeSrc"
|
2025-05-18 10:33:02 +08:00
|
|
|
|
:key="getTabKey(route)"
|
2025-04-08 20:27:03 +08:00
|
|
|
|
/>
|
|
|
|
|
</KeepAlive>
|
|
|
|
|
<component
|
|
|
|
|
:is="Component"
|
|
|
|
|
v-else-if="renderRouteView"
|
2025-05-18 10:33:02 +08:00
|
|
|
|
:key="getTabKey(route)"
|
2025-04-08 20:27:03 +08:00
|
|
|
|
/>
|
|
|
|
|
</template>
|
2024-06-23 19:39:44 +08:00
|
|
|
|
</RouterView>
|
|
|
|
|
</div>
|
2024-05-19 21:20:42 +08:00
|
|
|
|
</template>
|