index.vue 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <template>
  2. <el-breadcrumb class="app-breadcrumb" separator-class="el-icon-arrow-right">
  3. <transition-group name="breadcrumb">
  4. <el-breadcrumb-item v-for="(item, index) in breadcrumbs" :key="item.path">
  5. <span v-if="item.redirect === 'noredirect' || index === breadcrumbs.length - 1" class="no-redirect">{{
  6. generateTitle(item.meta.title)
  7. }}</span>
  8. <a v-else @click.prevent="handleLink(item)">
  9. {{ generateTitle(item.meta.title) }}
  10. </a>
  11. </el-breadcrumb-item>
  12. </transition-group>
  13. </el-breadcrumb>
  14. </template>
  15. <script setup lang="ts">
  16. import { onBeforeMount, ref, watch } from 'vue'
  17. import { useRoute, RouteLocationMatched } from 'vue-router'
  18. // import { compile } from 'path-to-regexp'
  19. import router from '@/router'
  20. import { generateTitle } from '@/utils/i18n'
  21. const currentRoute = useRoute()
  22. // const pathCompile = (path: string) => {
  23. // const { params } = currentRoute
  24. // const toPath = compile(path)
  25. // return toPath(params)
  26. // }
  27. const breadcrumbs = ref([] as Array<RouteLocationMatched>)
  28. function getBreadcrumb() {
  29. // console.error(currentRoute, 'currentRoute')
  30. let matched = currentRoute.matched.filter(item => item.meta && item.meta.title)
  31. const first = matched[0]
  32. if (!isDashboard(first)) {
  33. matched = [{ path: '/dashboard', meta: { title: 'dashboard' } } as any].concat(matched)
  34. }
  35. breadcrumbs.value = matched
  36. // .filter(item => {
  37. // return item.meta && item.meta.title && item.meta.breadcrumb !== false
  38. // })
  39. }
  40. function isDashboard(route: RouteLocationMatched) {
  41. const name = route && route.name
  42. if (!name) {
  43. return false
  44. }
  45. return name.toString().trim().toLocaleLowerCase() === 'Dashboard'.toLocaleLowerCase()
  46. }
  47. function handleLink(item: any) {
  48. const { redirect, path } = item
  49. if (redirect) {
  50. router.push(redirect).catch(err => {
  51. console.warn(err)
  52. })
  53. return
  54. }
  55. // router.push(pathCompile(path)).catch(err => {
  56. router.push(path).catch(err => {
  57. console.warn(err)
  58. })
  59. }
  60. watch(
  61. () => currentRoute.path,
  62. path => {
  63. if (path.startsWith('/redirect/')) {
  64. return
  65. }
  66. getBreadcrumb()
  67. }
  68. )
  69. onBeforeMount(() => {
  70. getBreadcrumb()
  71. })
  72. </script>
  73. <style lang="scss" scoped>
  74. /*
  75. .app-breadcrumb.el-breadcrumb {
  76. display: inline-block;
  77. font-size: 14px;
  78. line-height: 50px;
  79. margin-left: 8px;
  80. .no-redirect {
  81. color: #97a8be;
  82. cursor: text;
  83. }
  84. }*/
  85. </style>