index.vue 1.8 KB

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