LayoutBreadcrumb.tsx 2.9 KB

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