THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

79 lines
2.0 KiB

  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: 'Dashboard' }}].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>