breadCrumb.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <script setup lang="ts">
  2. import { ref, watch } from "vue";
  3. import { transformI18n } from "/@/plugins/i18n";
  4. import { isEqual, findIndex } from "lodash-es";
  5. import { useMultiTagsStoreHook } from "/@/store/modules/multiTags";
  6. import { getParentPaths, findRouteByPath } from "/@/router/utils";
  7. import { useRoute, useRouter, RouteLocationMatched } from "vue-router";
  8. const levelList = ref([]);
  9. const route = useRoute();
  10. const router = useRouter();
  11. const routes = router.options.routes;
  12. const multiTags = useMultiTagsStoreHook().multiTags;
  13. const isDashboard = (route: RouteLocationMatched): boolean | string => {
  14. const name = route && (route.name as string);
  15. if (!name) {
  16. return false;
  17. }
  18. return name.trim().toLocaleLowerCase() === "welcome".toLocaleLowerCase();
  19. };
  20. // 获取动态路由信息
  21. const getDynamicRoute = (path, tags) => {
  22. const dynamicRoute = findRouteByPath(path, tags);
  23. if (!dynamicRoute) {
  24. return null;
  25. } else if (isEqual(dynamicRoute.query, route.query)) {
  26. return dynamicRoute;
  27. } else {
  28. const index = findIndex(
  29. tags,
  30. v => v.path === dynamicRoute.path && isEqual(v.query, dynamicRoute.query)
  31. );
  32. // 去掉不符合当前路由query的路由
  33. const newTags = tags.slice(index + 1);
  34. return getDynamicRoute(path, newTags);
  35. }
  36. };
  37. const getBreadcrumb = (): void => {
  38. // 当前路由信息
  39. let currentRoute;
  40. if (route.meta?.realPath) {
  41. currentRoute = getDynamicRoute(route.path, multiTags);
  42. } else {
  43. currentRoute = findRouteByPath(route.path, multiTags);
  44. }
  45. // 当前路由的父级路径组成的数组
  46. const parentRoutes = getParentPaths(route.path, routes);
  47. // 存放组成面包屑的数组
  48. let matched = [];
  49. // 获取每个父级路径对应的路由信息
  50. parentRoutes.forEach(path => {
  51. if (path !== "/") {
  52. matched.push(findRouteByPath(path, routes));
  53. }
  54. });
  55. if (route.meta.refreshRedirect) {
  56. matched.unshift(
  57. findRouteByPath(route.meta.refreshRedirect as string, routes)
  58. );
  59. } else {
  60. // 过滤与子级相同标题的父级路由
  61. matched = matched.filter(item => {
  62. return !item.redirect || (item.redirect && item.children.length !== 1);
  63. });
  64. }
  65. if (currentRoute.path !== "/welcome") {
  66. matched.push(currentRoute);
  67. }
  68. const first = matched[0];
  69. if (!isDashboard(first)) {
  70. matched = [
  71. {
  72. path: "/welcome",
  73. parentPath: "/",
  74. meta: { title: "message.hshome", i18n: true }
  75. } as unknown as RouteLocationMatched
  76. ].concat(matched);
  77. }
  78. levelList.value = matched.filter(
  79. item => item.meta && item.meta.title && item.meta.breadcrumb !== false
  80. );
  81. };
  82. getBreadcrumb();
  83. watch(
  84. () => route.path,
  85. () => getBreadcrumb()
  86. );
  87. const handleLink = (item: RouteLocationMatched): any => {
  88. const { redirect, path } = item;
  89. if (redirect) {
  90. router.push(redirect.toString());
  91. return;
  92. }
  93. router.push(path);
  94. };
  95. </script>
  96. <template>
  97. <el-breadcrumb class="app-breadcrumb" separator="/">
  98. <transition-group appear name="breadcrumb">
  99. <el-breadcrumb-item v-for="(item, index) in levelList" :key="item.path">
  100. <span
  101. v-if="item.redirect === 'noRedirect' || index == levelList.length - 1"
  102. class="no-redirect"
  103. >{{ transformI18n(item.meta.title, item.meta.i18n) }}</span
  104. >
  105. <a v-else @click.prevent="handleLink(item)">
  106. {{ transformI18n(item.meta.title, item.meta.i18n) }}
  107. </a>
  108. </el-breadcrumb-item>
  109. </transition-group>
  110. </el-breadcrumb>
  111. </template>
  112. <style lang="scss" scoped>
  113. .app-breadcrumb.el-breadcrumb {
  114. display: inline-block;
  115. font-size: 14px;
  116. line-height: 50px;
  117. .no-redirect {
  118. color: #97a8be;
  119. cursor: text;
  120. }
  121. }
  122. </style>