index.vue 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 (route.path.startsWith('/redirect/')) {
  21. return
  22. }
  23. this.getBreadcrumb()
  24. }
  25. },
  26. created() {
  27. this.getBreadcrumb()
  28. },
  29. methods: {
  30. getBreadcrumb() {
  31. let matched = this.$route.matched.filter(item => item.meta && item.meta.title)
  32. const first = matched[0]
  33. if (!this.isDashboard(first)) {
  34. matched = [{ path: '/dashboard', meta: { title: '首页' }}].concat(matched)
  35. }
  36. this.levelList = matched.filter(item => item.meta && item.meta.title && item.meta.breadcrumb !== false)
  37. },
  38. isDashboard(route) {
  39. const name = route && route.name
  40. if (!name) {
  41. return false
  42. }
  43. return name.trim() === 'Dashboard'
  44. },
  45. handleLink(item) {
  46. const { redirect, path } = item
  47. if (redirect) {
  48. this.$router.push(redirect)
  49. return
  50. }
  51. this.$router.push(path)
  52. }
  53. }
  54. }
  55. </script>
  56. <style lang="scss" scoped>
  57. .app-breadcrumb.el-breadcrumb {
  58. display: inline-block;
  59. font-size: 14px;
  60. line-height: 50px;
  61. margin-left: 8px;
  62. .no-redirect {
  63. color: #97a8be;
  64. cursor: text;
  65. }
  66. }
  67. </style>