index.vue 1.8 KB

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