breadcrumb.vue 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <script lang="ts" setup>
  2. import type { BreadcrumbStyleType } from '@vben/types';
  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/locales';
  7. import { VbenBreadcrumbView } 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 { meta, path } = match;
  26. const { hideChildrenInMenu, hideInBreadcrumb, icon, name, title } =
  27. meta || {};
  28. if (hideInBreadcrumb || hideChildrenInMenu || !path) {
  29. continue;
  30. }
  31. resultBreadcrumb.push({
  32. icon,
  33. path: path || route.path,
  34. title: title ? $t((title || name) as string) : '',
  35. });
  36. }
  37. if (props.showHome) {
  38. resultBreadcrumb.unshift({
  39. icon: 'mdi:home-outline',
  40. isHome: true,
  41. path: '/',
  42. });
  43. }
  44. if (props.hideWhenOnlyOne && resultBreadcrumb.length === 1) {
  45. return [];
  46. }
  47. return resultBreadcrumb;
  48. });
  49. function handleSelect(path: string) {
  50. router.push(path);
  51. }
  52. </script>
  53. <template>
  54. <VbenBreadcrumbView
  55. :breadcrumbs="breadcrumbs"
  56. :show-icon="showIcon"
  57. :style-type="type"
  58. class="ml-2"
  59. @select="handleSelect"
  60. />
  61. </template>