index.vue 1.7 KB

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