index.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. {{ generateTitle(item.meta.title) }}
  7. </span>
  8. <a v-else @click.prevent="handleLink(item)">{{ generateTitle(item.meta.title) }}</a>
  9. </el-breadcrumb-item>
  10. </transition-group>
  11. </el-breadcrumb>
  12. </template>
  13. <script>
  14. import { generateTitle } from '@/utils/i18n'
  15. import pathToRegexp from 'path-to-regexp'
  16. export default {
  17. data() {
  18. return {
  19. levelList: null
  20. }
  21. },
  22. watch: {
  23. $route(route) {
  24. // if you go to the redirect page, do not update the breadcrumbs
  25. if (route.path.startsWith('/redirect/')) {
  26. return
  27. }
  28. this.getBreadcrumb()
  29. }
  30. },
  31. created() {
  32. this.getBreadcrumb()
  33. },
  34. methods: {
  35. generateTitle,
  36. getBreadcrumb() {
  37. // only show routes with meta.title
  38. let matched = this.$route.matched.filter(item => item.meta && item.meta.title)
  39. const first = matched[0]
  40. if (!this.isDashboard(first)) {
  41. matched = [{ path: '/dashboard', meta: { title: 'dashboard' }}].concat(matched)
  42. }
  43. this.levelList = matched.filter(item => item.meta && item.meta.title && item.meta.breadcrumb !== false)
  44. },
  45. isDashboard(route) {
  46. const name = route && route.name
  47. if (!name) {
  48. return false
  49. }
  50. return name.trim().toLocaleLowerCase() === 'Dashboard'.toLocaleLowerCase()
  51. },
  52. pathCompile(path) {
  53. // To solve this problem https://github.com/PanJiaChen/vue-element-admin/issues/561
  54. const { params } = this.$route
  55. var toPath = pathToRegexp.compile(path)
  56. return toPath(params)
  57. },
  58. handleLink(item) {
  59. const { redirect, path } = item
  60. if (redirect) {
  61. this.$router.push(redirect)
  62. return
  63. }
  64. this.$router.push(this.pathCompile(path))
  65. }
  66. }
  67. }
  68. </script>
  69. <style lang="scss" scoped>
  70. .app-breadcrumb.el-breadcrumb {
  71. display: inline-block;
  72. font-size: 14px;
  73. line-height: 50px;
  74. margin-left: 8px;
  75. .no-redirect {
  76. color: #97a8be;
  77. cursor: text;
  78. }
  79. }
  80. </style>