breadcrumb.vue 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <script lang="ts" setup>
  2. import type { IBreadcrumb } from '@vben-core/shadcn-ui';
  3. import { VbenBackgroundBreadcrumb, VbenBreadcrumb } from '@vben-core/shadcn-ui';
  4. import { BreadcrumbStyle } from '@vben-core/typings';
  5. import { $t } from '@vben/locales';
  6. import { computed } from 'vue';
  7. import { useRoute, useRouter } from 'vue-router';
  8. interface Props {
  9. hideWhenOnlyOne?: boolean;
  10. showHome?: boolean;
  11. showIcon?: boolean;
  12. type?: BreadcrumbStyle;
  13. }
  14. const props = withDefaults(defineProps<Props>(), {
  15. showHome: true,
  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. class="ml-2"
  69. :show-icon="showIcon"
  70. @select="handleSelect"
  71. />
  72. <VbenBackgroundBreadcrumb
  73. v-if="type === 'background'"
  74. :breadcrumbs="breadcrumbs"
  75. class="ml-2"
  76. :show-icon="showIcon"
  77. @select="handleSelect"
  78. />
  79. </template>