9ec295e2c114b7743b3d993746e632e297e18a7b.svn-base 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. import pathToRegexp from 'path-to-regexp'
  13. export default {
  14. data() {
  15. return {
  16. levelList: null
  17. }
  18. },
  19. watch: {
  20. $route() {
  21. this.getBreadcrumb()
  22. }
  23. },
  24. created() {
  25. this.getBreadcrumb()
  26. },
  27. methods: {
  28. getBreadcrumb() {
  29. // only show routes with meta.title
  30. let matched = this.$route.matched.filter(item => item.meta && item.meta.title)
  31. const first = matched[0]
  32. if (!this.isDashboard(first)) {
  33. matched = [{ path: '/dashboard', meta: { title: '首页' }}].concat(matched)
  34. }
  35. this.levelList = matched.filter(item => item.meta && item.meta.title && item.meta.breadcrumb !== false)
  36. },
  37. isDashboard(route) {
  38. const name = route && route.name
  39. if (!name) {
  40. return false
  41. }
  42. return name.trim().toLocaleLowerCase() === 'Dashboard'.toLocaleLowerCase()
  43. },
  44. pathCompile(path) {
  45. // To solve this problem https://github.com/PanJiaChen/vue-element-admin/issues/561
  46. const { params } = this.$route
  47. var toPath = pathToRegexp.compile(path)
  48. return toPath(params)
  49. },
  50. handleLink(item) {
  51. const { redirect, path } = item
  52. if (redirect) {
  53. this.$router.push(redirect)
  54. return
  55. }
  56. this.$router.push(this.pathCompile(path))
  57. }
  58. }
  59. }
  60. </script>
  61. <style lang="scss" scoped>
  62. .app-breadcrumb.el-breadcrumb {
  63. display: inline-block;
  64. font-size: 14px;
  65. line-height: 50px;
  66. margin-left: 8px;
  67. .no-redirect {
  68. color: #97a8be;
  69. cursor: text;
  70. }
  71. }
  72. </style>