index.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <template>
  2. <div class="global-breadcrumb">
  3. <a-breadcrumb>
  4. <a-breadcrumb-item v-for="(item, index) in breadcrumbItems" :key="index">
  5. <span
  6. v-if="index === breadcrumbItems.length - 1"
  7. class="breadcrumb-text"
  8. >
  9. {{ item.title }}
  10. </span>
  11. <a-link
  12. v-else
  13. @click="goToRoute(item.defRouter)"
  14. :hoverable="false"
  15. class="breadcrumb-link"
  16. >
  17. {{ item.title }}
  18. </a-link>
  19. </a-breadcrumb-item>
  20. </a-breadcrumb>
  21. </div>
  22. </template>
  23. <script name="GlobalBreadcrumb" lang="ts" setup>
  24. import { ref, watch } from 'vue';
  25. import { useRoute, useRouter } from 'vue-router';
  26. import { useI18n } from 'vue-i18n';
  27. const route = useRoute();
  28. const router = useRouter();
  29. const { t } = useI18n();
  30. const breadcrumbItems = ref<
  31. { title: string; path: string; defRouter: string }[]
  32. >([]);
  33. // 获取面包屑项
  34. const getBreadcrumbItems = () => {
  35. const matched = route.matched.filter(item => {
  36. return item.meta && item.meta.locale;
  37. });
  38. const items = matched.map(item => {
  39. return {
  40. title: t(item.meta.locale as string),
  41. path: item.path,
  42. defRouter: item.children[0]?.path || item?.path,
  43. };
  44. });
  45. return items;
  46. };
  47. // 监听路由变化更新面包屑
  48. watch(
  49. () => route.matched,
  50. () => {
  51. breadcrumbItems.value = getBreadcrumbItems();
  52. },
  53. { immediate: true }
  54. );
  55. // 跳转到指定路由
  56. const goToRoute = (path: string) => {
  57. console.log('path', path);
  58. if (path) {
  59. router.push(path);
  60. }
  61. };
  62. </script>
  63. <style scoped lang="less">
  64. .global-breadcrumb {
  65. margin-left: 16px;
  66. :deep(.arco-breadcrumb) {
  67. display: flex;
  68. align-items: center;
  69. height: 50px;
  70. }
  71. :deep(.arco-breadcrumb-item) {
  72. display: flex;
  73. align-items: center;
  74. &:last-child .arco-breadcrumb-item-inner {
  75. color: var(--color-text-1);
  76. font-weight: 500;
  77. }
  78. }
  79. }
  80. .breadcrumb-link {
  81. color: rgb(var(--color-text-1));
  82. text-decoration: none;
  83. cursor: pointer;
  84. &:hover {
  85. color: rgb(var(--color-text-4));
  86. // text-decoration: underline;
  87. }
  88. }
  89. .breadcrumb-text {
  90. color: var(--color-text-1);
  91. }
  92. </style>