index.vue 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <!-- @format -->
  2. <template>
  3. <el-breadcrumb class="app-breadcrumb" separator="/">
  4. <transition-group name="breadcrumb">
  5. <el-breadcrumb-item v-for="(item, index) in levelList" :key="item.path">
  6. <span
  7. v-if="item.redirect === 'noRedirect' || index == levelList.length - 1"
  8. class="no-redirect"
  9. >{{ item.meta.title }}</span
  10. >
  11. <a v-else @click.prevent="handleLink(item)">{{ item.meta.title }}</a>
  12. </el-breadcrumb-item>
  13. </transition-group>
  14. </el-breadcrumb>
  15. </template>
  16. <script setup>
  17. const route = useRoute();
  18. const router = useRouter();
  19. const levelList = ref([]);
  20. function getBreadcrumb() {
  21. // only show routes with meta.title
  22. let matched = route.matched.filter((item) => item.meta && item.meta.title);
  23. const first = matched[0];
  24. // 判断是否为首页
  25. if (!isDashboard(first)) {
  26. matched = [{ path: "/index", meta: { title: "首页" } }].concat(matched);
  27. }
  28. levelList.value = matched.filter(
  29. (item) => item.meta && item.meta.title && item.meta.breadcrumb !== false
  30. );
  31. }
  32. function isDashboard(route) {
  33. const name = route && route.name;
  34. if (!name) {
  35. return false;
  36. }
  37. return name.trim() === "Index";
  38. }
  39. function handleLink(item) {
  40. const { redirect, path } = item;
  41. if (redirect) {
  42. router.push(redirect);
  43. return;
  44. }
  45. router.push(path);
  46. }
  47. watchEffect(() => {
  48. // if you go to the redirect page, do not update the breadcrumbs
  49. if (route.path.startsWith("/redirect/")) {
  50. return;
  51. }
  52. getBreadcrumb();
  53. });
  54. getBreadcrumb();
  55. </script>
  56. <style lang="scss" scoped>
  57. .app-breadcrumb.el-breadcrumb {
  58. display: inline-block;
  59. font-size: 14px;
  60. line-height: 50px;
  61. margin-left: 8px;
  62. .no-redirect {
  63. color: #fff;
  64. cursor: text;
  65. }
  66. }
  67. </style>