index.vue 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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>
  12. const route = useRoute();
  13. const router = useRouter();
  14. const levelList = ref([])
  15. function getBreadcrumb() {
  16. // only show routes with meta.title
  17. let matched = route.matched.filter(item => item.meta && item.meta.title);
  18. const first = matched[0]
  19. // 判断是否为首页
  20. if (!isDashboard(first)) {
  21. matched = [{ path: '/index', meta: { title: '首页' } }].concat(matched)
  22. }
  23. levelList.value = matched.filter(item => item.meta && item.meta.title && item.meta.breadcrumb !== false)
  24. }
  25. function isDashboard(route) {
  26. const name = route && route.name
  27. if (!name) {
  28. return false
  29. }
  30. return name.trim() === 'Index'
  31. }
  32. function handleLink(item) {
  33. const { redirect, path } = item
  34. if (redirect) {
  35. router.push(redirect)
  36. return
  37. }
  38. router.push(path)
  39. }
  40. watchEffect(() => {
  41. // if you go to the redirect page, do not update the breadcrumbs
  42. if (route.path.startsWith('/redirect/')) {
  43. return
  44. }
  45. getBreadcrumb()
  46. })
  47. getBreadcrumb();
  48. </script>
  49. <style lang='scss' scoped>
  50. .app-breadcrumb.el-breadcrumb {
  51. display: inline-block;
  52. font-size: 14px;
  53. line-height: 50px;
  54. margin-left: 8px;
  55. .no-redirect {
  56. color: #97a8be;
  57. cursor: text;
  58. }
  59. }
  60. </style>