index.vue 1.9 KB

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