LayoutBreadcrumb.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import type { AppRouteRecordRaw } from '/@/router/types';
  2. import type { RouteLocationMatched } from 'vue-router';
  3. import type { PropType } from 'vue';
  4. import { defineComponent, TransitionGroup, unref, watch, ref } from 'vue';
  5. import Breadcrumb from '/@/components/Breadcrumb/Breadcrumb.vue';
  6. import BreadcrumbItem from '/@/components/Breadcrumb/BreadcrumbItem.vue';
  7. import { useRouter } from 'vue-router';
  8. import router from '/@/router';
  9. import { PageEnum } from '/@/enums/pageEnum';
  10. import { isBoolean } from '/@/utils/is';
  11. import { compile } from 'path-to-regexp';
  12. import Icon from '/@/components/Icon';
  13. export default defineComponent({
  14. name: 'BasicBreadcrumb',
  15. props: {
  16. showIcon: {
  17. type: Boolean as PropType<boolean>,
  18. default: false,
  19. },
  20. },
  21. setup(props) {
  22. const itemList = ref<AppRouteRecordRaw[]>([]);
  23. const { currentRoute, push } = useRouter();
  24. watch(
  25. () => currentRoute.value,
  26. () => {
  27. if (unref(currentRoute).name === 'Redirect') return;
  28. getBreadcrumb();
  29. },
  30. { immediate: true }
  31. );
  32. function getBreadcrumb() {
  33. const { matched } = unref(currentRoute);
  34. const matchedList = matched.filter((item) => item.meta && item.meta.title).slice(1);
  35. const firstItem = matchedList[0];
  36. const ret = getHomeRoute(firstItem);
  37. if (!isBoolean(ret)) {
  38. matchedList.unshift(ret);
  39. }
  40. itemList.value = ((matchedList as any) as AppRouteRecordRaw[]).filter(
  41. (item) => item.meta && item.meta.title && !item.meta.hideBreadcrumb
  42. );
  43. }
  44. function getHomeRoute(firstItem: RouteLocationMatched) {
  45. if (!firstItem || !firstItem.name) return false;
  46. const routes = router.getRoutes();
  47. const homeRoute = routes.find((item) => item.path === PageEnum.BASE_HOME);
  48. if (!homeRoute) return false;
  49. if (homeRoute.name === firstItem.name) return false;
  50. return homeRoute;
  51. }
  52. function pathCompile(path: string) {
  53. const { params } = unref(currentRoute);
  54. const toPath = compile(path);
  55. return toPath(params);
  56. }
  57. function handleItemClick(item: AppRouteRecordRaw) {
  58. const { redirect, path, meta } = item;
  59. if (meta.disabledRedirect) return;
  60. if (redirect) {
  61. push(redirect as string);
  62. return;
  63. }
  64. return push(pathCompile(path));
  65. }
  66. return () => (
  67. <Breadcrumb class={['layout-breadcrumb', unref(itemList).length === 0 ? 'hidden' : '']}>
  68. {() => (
  69. <TransitionGroup name="breadcrumb">
  70. {() => {
  71. return unref(itemList).map((item) => {
  72. const isLink =
  73. (!!item.redirect && !item.meta.disabledRedirect) ||
  74. !item.children ||
  75. item.children.length === 0;
  76. return (
  77. <BreadcrumbItem
  78. key={item.path}
  79. isLink={isLink}
  80. onClick={handleItemClick.bind(null, item)}
  81. >
  82. {() => (
  83. <>
  84. {props.showIcon && item.meta.icon && item.meta.icon.trim() !== '' && (
  85. <Icon
  86. icon={item.meta.icon}
  87. class="icon mr-1 "
  88. style={{
  89. marginBottom: '2px',
  90. }}
  91. />
  92. )}
  93. {item.meta.title}
  94. </>
  95. )}
  96. </BreadcrumbItem>
  97. );
  98. });
  99. }}
  100. </TransitionGroup>
  101. )}
  102. </Breadcrumb>
  103. );
  104. },
  105. });