index.vue 1.8 KB

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