gaoluyang
2026-06-24 712aa51536236d43e87273e4ce45ac5691dffad8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import type { VNode } from 'vue';
import type {
  RouteLocationNormalizedLoaded,
  RouteLocationNormalizedLoadedGeneric,
} from 'vue-router';
 
import { computed } from 'vue';
 
import { preferences, usePreferences } from '..\..\..\..\preferences\src';
 
/**
 * 转换组件,自动添加 name
 * @param component
 * @param route
 */
export function transformComponent(
  component: VNode,
  route: RouteLocationNormalizedLoadedGeneric,
) {
  // 组件视图未找到,如果有设置后备视图,则返回后备视图,如果没有,则抛出错误
  if (!component) {
    console.error(
      'Component view not found,please check the route configuration',
    );
    return undefined;
  }
 
  const routeName = route.name as string;
  // 如果组件没有 name,则直接返回
  if (!routeName) {
    return component;
  }
  const componentName = (component?.type as any)?.name;
 
  // 已经设置过 name,则直接返回
  if (componentName) {
    return component;
  }
 
  // componentName 与 routeName 一致,则直接返回
  if (componentName === routeName) {
    return component;
  }
 
  // 设置 name
  component.type ||= {};
  (component.type as any).name = routeName;
 
  return component;
}
 
/**
 * Layout相关hook
 */
export function useLayoutHook() {
  const { keepAlive } = usePreferences();
  /**
   * 是否使用动画
   */
  const getEnabledTransition = computed(() => {
    const { transition } = preferences;
    const transitionName = transition.name;
    return transitionName && transition.enable;
  });
 
  /**
   * 获取路由过渡动画
   * @param _route
   */
  function getTransitionName(_route: RouteLocationNormalizedLoaded) {
    // 如果偏好设置未设置,则不使用动画
    const { tabbar, transition } = preferences;
    const transitionName = transition.name;
    if (!transitionName || !transition.enable) {
      return;
    }
 
    // 标签页未启用或者未开启缓存,则使用全局配置动画
    if (!tabbar.enable || !keepAlive) {
      return transitionName;
    }
 
    // 如果页面已经加载过,则不使用动画
    // if (route.meta.loaded) {
    //   return;
    // }
    // 已经打开且已经加载过的页面不使用动画
    // const inTabs = getCachedTabs.value.includes(route.name as string);
 
    // return inTabs && route.meta.loaded ? undefined : transitionName;
    return transitionName;
  }
 
  return {
    getEnabledTransition,
    getTransitionName,
  };
}