index.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.defaultRoutes, matched)
  44. } else {
  45. matched = router.matched.filter(item => item.meta && item.meta.title)
  46. }
  47. // 判断是否为首页
  48. if (!this.isDashboard(matched[0])) {
  49. matched = [{ path: "/index", meta: { title: "首页" } }].concat(matched)
  50. }
  51. this.levelList = matched.filter(item => item.meta && item.meta.title && item.meta.breadcrumb !== false)
  52. },
  53. findPathNum(str, char = "/") {
  54. let index = str.indexOf(char)
  55. let num = 0
  56. while (index !== -1) {
  57. num++
  58. index = str.indexOf(char, index + 1)
  59. }
  60. return num
  61. },
  62. getMatched(pathList, routeList, matched) {
  63. let data = routeList.find(item => item.path == pathList[0] || (item.name += '').toLowerCase() == pathList[0])
  64. if (data) {
  65. matched.push(data)
  66. if (data.children && pathList.length) {
  67. pathList.shift()
  68. this.getMatched(pathList, data.children, matched)
  69. }
  70. }
  71. },
  72. isDashboard(route) {
  73. const name = route && route.name
  74. if (!name) {
  75. return false
  76. }
  77. return name.trim() === 'Index'
  78. },
  79. handleLink(item) {
  80. const { redirect, path } = item
  81. if (redirect) {
  82. this.$router.push(redirect)
  83. return
  84. }
  85. this.$router.push(path)
  86. }
  87. }
  88. }
  89. </script>
  90. <style lang="scss" scoped>
  91. .app-breadcrumb.el-breadcrumb {
  92. display: inline-block;
  93. font-size: 14px;
  94. line-height: 50px;
  95. margin-left: 8px;
  96. .no-redirect {
  97. color: #97a8be;
  98. cursor: text;
  99. }
  100. }
  101. </style>