index.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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">{{ item.meta.title }}</span>
  6. <a v-else @click.prevent="handleLink(item)">{{ item.meta.title }}</a>
  7. </el-breadcrumb-item>
  8. </transition-group>
  9. </el-breadcrumb>
  10. </template>
  11. <script>
  12. export default {
  13. data() {
  14. return {
  15. levelList: null
  16. }
  17. },
  18. watch: {
  19. $route(route) {
  20. // if you go to the redirect page, do not update the breadcrumbs
  21. if (route.path.startsWith('/redirect/')) {
  22. return
  23. }
  24. this.getBreadcrumb()
  25. }
  26. },
  27. created() {
  28. this.getBreadcrumb()
  29. },
  30. methods: {
  31. getBreadcrumb() {
  32. // only show routes with meta.title
  33. let matched = []
  34. const router = this.$route
  35. const pathNum = this.findPathNum(router.path)
  36. // multi-level menu
  37. if (pathNum > 2) {
  38. const reg = /\/\w+/gi
  39. const pathList = router.path.match(reg).map((item, index) => {
  40. if (index !== 0) item = item.slice(1)
  41. return item
  42. })
  43. this.getMatched(pathList, this.$store.getters.sidebarRouters, matched)
  44. } else {
  45. matched = router.matched.filter((item) => item.meta && item.meta.title)
  46. }
  47. if (!this.isDashboard(matched[0])) {
  48. matched = [{ path: "/index", meta: { title: "首页" } }].concat(matched)
  49. }
  50. this.levelList = matched.filter((item) => item.meta && item.meta.title && item.meta.breadcrumb !== false)
  51. },
  52. findPathNum(str, char = "/") {
  53. let index = str.indexOf(char)
  54. let num = 0
  55. while (index !== -1) {
  56. num++
  57. index = str.indexOf(char, index + 1)
  58. }
  59. return num
  60. },
  61. getMatched(pathList, routeList, matched) {
  62. let data = routeList.find((item) => item.path == pathList[0])
  63. matched.push(data)
  64. if (data.children && pathList.length) {
  65. pathList.shift()
  66. this.getMatched(pathList, data.children, matched)
  67. }
  68. },
  69. isDashboard(route) {
  70. const name = route && route.name
  71. if (!name) {
  72. return false
  73. }
  74. return name.trim() === 'Index'
  75. },
  76. handleLink(item) {
  77. const { redirect, path } = item
  78. if (redirect) {
  79. this.$router.push(redirect)
  80. return
  81. }
  82. this.$router.push(path)
  83. }
  84. }
  85. }
  86. </script>
  87. <style lang="scss" scoped>
  88. .app-breadcrumb.el-breadcrumb {
  89. display: inline-block;
  90. font-size: 14px;
  91. line-height: 50px;
  92. margin-left: 8px;
  93. .no-redirect {
  94. color: #97a8be;
  95. cursor: text;
  96. }
  97. }
  98. </style>