index.vue 1.8 KB

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