You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.0 KiB

  1. <script setup lang="ts">
  2. import { ref, watch } from "vue";
  3. import { useRoute, useRouter, RouteLocationMatched } from "vue-router";
  4. import { transformI18n } from "/@/plugins/i18n";
  5. const levelList = ref([]);
  6. const route = useRoute();
  7. const router = useRouter();
  8. const isDashboard = (route: RouteLocationMatched): boolean | string => {
  9. const name = route && (route.name as string);
  10. if (!name) {
  11. return false;
  12. }
  13. return name.trim().toLocaleLowerCase() === "welcome".toLocaleLowerCase();
  14. };
  15. const getBreadcrumb = (): void => {
  16. let matched = route.matched.filter(item => item.meta && item.meta.title);
  17. const first = matched[0];
  18. if (!isDashboard(first)) {
  19. matched = [
  20. {
  21. path: "/welcome",
  22. parentPath: "/",
  23. meta: { title: "message.hshome", i18n: true }
  24. } as unknown as RouteLocationMatched
  25. ].concat(matched);
  26. }
  27. levelList.value = matched.filter(
  28. item => item.meta && item.meta.title && item.meta.breadcrumb !== false
  29. );
  30. };
  31. getBreadcrumb();
  32. watch(
  33. () => route.path,
  34. () => getBreadcrumb()
  35. );
  36. const handleLink = (item: RouteLocationMatched): any => {
  37. const { redirect, path } = item;
  38. if (redirect) {
  39. router.push(redirect.toString());
  40. return;
  41. }
  42. router.push(path);
  43. };
  44. </script>
  45. <template>
  46. <el-breadcrumb class="app-breadcrumb" separator="/">
  47. <transition-group appear name="breadcrumb">
  48. <el-breadcrumb-item v-for="(item, index) in levelList" :key="item.path">
  49. <span
  50. v-if="item.redirect === 'noRedirect' || index == levelList.length - 1"
  51. class="no-redirect"
  52. >{{ transformI18n(item.meta.title, item.meta.i18n) }}</span
  53. >
  54. <a v-else @click.prevent="handleLink(item)">
  55. {{ transformI18n(item.meta.title, item.meta.i18n) }}
  56. </a>
  57. </el-breadcrumb-item>
  58. </transition-group>
  59. </el-breadcrumb>
  60. </template>
  61. <style lang="scss" scoped>
  62. .app-breadcrumb.el-breadcrumb {
  63. display: inline-block;
  64. font-size: 14px;
  65. line-height: 50px;
  66. .no-redirect {
  67. color: #97a8be;
  68. cursor: text;
  69. }
  70. }
  71. </style>