BreadCrumbs.vue 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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="index == 0">
  6. <i class="el-icon-s-home"></i>
  7. </span>
  8. <span v-if="item.redirect === 'noRedirect' || index == levelList.length - 1" class="no-redirect">{{ item.meta.title }}</span>
  9. <a v-else @click.prevent="handleLink(item)">{{ item.meta.title }}</a>
  10. </el-breadcrumb-item>
  11. <el-breadcrumb-item key="ext_item" v-if="hasExtItem">
  12. <slot name="ext_item"></slot>
  13. </el-breadcrumb-item>
  14. </transition-group>
  15. </el-breadcrumb>
  16. </template>
  17. <script>
  18. import { compile } from "path-to-regexp"
  19. export default {
  20. props: {
  21. hasExtItem: false
  22. },
  23. data() {
  24. return {
  25. levelList: null
  26. }
  27. },
  28. watch: {
  29. $route() {
  30. this.getBreadcrumb()
  31. }
  32. },
  33. created() {
  34. this.getBreadcrumb()
  35. },
  36. methods: {
  37. getBreadcrumb() {
  38. // only show routes with meta.title
  39. let matched = this.$route.matched.filter(item => item.meta && item.meta.title)
  40. const first = matched[0]
  41. if (!this.isHome(first)) {
  42. matched = [{ path: "/index", meta: { title: "首页" } }].concat(matched)
  43. }
  44. this.levelList = matched.filter(item => item.meta && item.meta.title && item.meta.breadcrumb !== false)
  45. },
  46. isHome(route) {
  47. const name = route && route.name
  48. if (!name) {
  49. return false
  50. }
  51. return name.trim().toLocaleLowerCase() === "index".toLocaleLowerCase()
  52. },
  53. pathCompile(path) {
  54. const { params } = this.$route
  55. var toPath = 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. margin: 10px auto 1px auto;
  74. width: 100%;
  75. border-width: 1px;
  76. border-style: solid;
  77. line-height: 30px;
  78. border-color: #eae9e9;
  79. .el-breadcrumb__item {
  80. padding-left: 10px;
  81. }
  82. .no-redirect {
  83. color: #97a8be;
  84. cursor: text;
  85. }
  86. }
  87. </style>