breadcrumb.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <script lang="ts" setup>
  2. import type { BreadcrumbStyleType } from '@vben-core/preferences';
  3. import type { IBreadcrumb } from '@vben-core/shadcn-ui';
  4. import { computed } from 'vue';
  5. import { useRoute, useRouter } from 'vue-router';
  6. import { $t } from '@vben-core/locales';
  7. import { VbenBackgroundBreadcrumb, VbenBreadcrumb } from '@vben-core/shadcn-ui';
  8. interface Props {
  9. hideWhenOnlyOne?: boolean;
  10. showHome?: boolean;
  11. showIcon?: boolean;
  12. type?: BreadcrumbStyleType;
  13. }
  14. const props = withDefaults(defineProps<Props>(), {
  15. showHome: false,
  16. showIcon: false,
  17. type: 'normal',
  18. });
  19. const route = useRoute();
  20. const router = useRouter();
  21. const breadcrumbs = computed((): IBreadcrumb[] => {
  22. const matched = route.matched;
  23. const resultBreadcrumb: IBreadcrumb[] = [];
  24. for (const match of matched) {
  25. const {
  26. meta,
  27. path,
  28. // children = []
  29. } = match;
  30. const { hideChildrenInMenu, hideInBreadcrumb, icon, name, title } =
  31. meta || {};
  32. if (hideInBreadcrumb || hideChildrenInMenu || !path) {
  33. continue;
  34. }
  35. resultBreadcrumb.push({
  36. icon: icon as string,
  37. path: path || route.path,
  38. title: $t((title || name) as string),
  39. // items: children.map((child) => {
  40. // return {
  41. // icon: child?.meta?.icon as string,
  42. // path: child.path,
  43. // title: child?.meta?.title as string,
  44. // };
  45. // }),
  46. });
  47. }
  48. if (props.showHome) {
  49. resultBreadcrumb.unshift({
  50. icon: 'mdi:home-outline',
  51. isHome: true,
  52. path: '/',
  53. });
  54. }
  55. if (props.hideWhenOnlyOne && resultBreadcrumb.length === 1) {
  56. return [];
  57. }
  58. return resultBreadcrumb;
  59. });
  60. function handleSelect(path: string) {
  61. router.push(path);
  62. }
  63. </script>
  64. <template>
  65. <VbenBreadcrumb
  66. v-if="type === 'normal'"
  67. :breadcrumbs="breadcrumbs"
  68. :show-icon="showIcon"
  69. class="ml-2"
  70. @select="handleSelect"
  71. />
  72. <VbenBackgroundBreadcrumb
  73. v-if="type === 'background'"
  74. :breadcrumbs="breadcrumbs"
  75. :show-icon="showIcon"
  76. class="ml-2"
  77. @select="handleSelect"
  78. />
  79. </template>