瀚海黎明企业官网后台
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.

63 lines
1.8 KiB

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