index.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. <template v-if="item.path === '/dashboard'">
  10. <img :src="homeIcon" alt="首页图标" style="height: 20px; vertical-align: middle;">
  11. </template>
  12. <template v-else>
  13. <span v-if="item.redirect === 'noRedirect' || index === levelList.length - 1" class="no-redirect">
  14. {{ generateTitle(item.meta.title) }}
  15. </span>
  16. <a v-else @click.prevent="handleLink(item)">{{ generateTitle(item.meta.title) }}</a>
  17. </template>
  18. </el-breadcrumb-item>
  19. </transition-group>
  20. </el-breadcrumb>
  21. </template>
  22. <script>
  23. import { generateTitle } from '@/utils/i18n'
  24. import pathToRegexp from 'path-to-regexp'
  25. export default {
  26. data() {
  27. return {
  28. levelList: null,
  29. homeIcon: require('@/assets/public/nav/Home.png') //引入首页图片路径
  30. }
  31. },
  32. watch: {
  33. $route(route) {
  34. // if you go to the redirect page, do not update the breadcrumbs
  35. if (route.path.startsWith('/redirect/')) {
  36. return
  37. }
  38. this.getBreadcrumb()
  39. }
  40. },
  41. created() {
  42. this.getBreadcrumb()
  43. },
  44. methods: {
  45. generateTitle,
  46. getBreadcrumb() {
  47. // only show routes with meta.title
  48. let matched = this.$route.matched.filter(item => item.meta && item.meta.title)
  49. const first = matched[0]
  50. // if (!this.isDashboard(first)) {
  51. // matched = [{ path: '/dashboard', meta: { title: 'dashboard' }}].concat(matched)
  52. // }
  53. if (!this.isDashboard(first) && !matched.some(item => item.path === '/dashboard')) {
  54. matched = [{ path: '/dashboard', meta: { title: '首页' }}].concat(matched)
  55. }
  56. this.levelList = matched.filter(item => item.meta && item.meta.title && item.meta.breadcrumb !== false)
  57. },
  58. isDashboard(route) {
  59. const name = route && route.name
  60. if (!name) {
  61. return false
  62. }
  63. return name.trim().toLocaleLowerCase() === 'Dashboard'.toLocaleLowerCase()
  64. },
  65. pathCompile(path) {
  66. // To solve this problem https://github.com/PanJiaChen/vue-element-admin/issues/561
  67. const { params } = this.$route
  68. var toPath = pathToRegexp.compile(path)
  69. return toPath(params)
  70. },
  71. handleLink(item) {
  72. const { redirect, path } = item
  73. if (redirect) {
  74. this.$router.push(redirect)
  75. return
  76. }
  77. this.$router.push(this.pathCompile(path))
  78. }
  79. }
  80. }
  81. </script>
  82. <style lang="scss" scoped>
  83. .app-breadcrumb.el-breadcrumb {
  84. display: inline-block;
  85. font-size: 12px;
  86. margin-left: 30px;
  87. height:24px;
  88. line-height:24px;
  89. padding-top:2px;
  90. img {
  91. width:13px !important;
  92. height:13px !important;
  93. margin-top:-4px;
  94. }
  95. .no-redirect {
  96. color: #97a8be;
  97. cursor: text;
  98. }
  99. }
  100. </style>