BreadCrumbs.vue 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <template>
  2. <div class="breadcrumb-preview">
  3. <el-breadcrumb :style='{"fontSize":"inherit","lineHeight":"1"}' separator="●">
  4. <transition-group name="breadcrumb" class="box">
  5. <el-breadcrumb-item v-for="(item,index) in levelList" :key="item.path">
  6. <span v-if="item.redirect==='noRedirect'||index==levelList.length-1" class="no-redirect">{{ item.name }}</span>
  7. <a v-else @click.prevent="handleLink(item)">
  8. <span class="icon iconfont icon-home19" :style='{"margin":"0 2px","lineHeight":"1","fontSize":"inherit","color":"inherit","display":"none"}'></span>首页
  9. </a>
  10. </el-breadcrumb-item>
  11. </transition-group>
  12. </el-breadcrumb>
  13. </div>
  14. </template>
  15. <script>
  16. import pathToRegexp from 'path-to-regexp'
  17. import { generateTitle } from '@/utils/i18n'
  18. export default {
  19. data() {
  20. return {
  21. levelList: null
  22. }
  23. },
  24. watch: {
  25. $route() {
  26. this.getBreadcrumb()
  27. }
  28. },
  29. created() {
  30. this.getBreadcrumb()
  31. },
  32. methods: {
  33. generateTitle,
  34. getBreadcrumb() {
  35. // only show routes with meta.title
  36. let route = this.$route
  37. let matched = route.matched.filter(item => item.meta)
  38. const first = matched[0]
  39. matched = [{ path: '/index' }].concat(matched)
  40. this.levelList = matched.filter(item => item.meta)
  41. },
  42. isDashboard(route) {
  43. const name = route && route.name
  44. if (!name) {
  45. return false
  46. }
  47. return name.trim().toLocaleLowerCase() === 'Index'.toLocaleLowerCase()
  48. },
  49. pathCompile(path) {
  50. // To solve this problem https://github.com/PanJiaChen/vue-element-admin/issues/561
  51. const { params } = this.$route
  52. var toPath = pathToRegexp.compile(path)
  53. return toPath(params)
  54. },
  55. handleLink(item) {
  56. const { redirect, path } = item
  57. if (redirect) {
  58. this.$router.push(redirect)
  59. return
  60. }
  61. if(path){
  62. this.$router.push(path)
  63. }else{
  64. this.$router.push('/')
  65. }
  66. },
  67. }
  68. }
  69. </script>
  70. <style lang="scss" scoped>
  71. .el-breadcrumb {
  72. & /deep/ .el-breadcrumb__separator {
  73. margin: 0 15px;
  74. color: #333;
  75. font-weight: 500;
  76. }
  77. & /deep/ .el-breadcrumb__inner a {
  78. color: #000;
  79. display: inline-block;
  80. }
  81. & /deep/ .el-breadcrumb__inner {
  82. color: #0977fd;
  83. display: inline-block;
  84. }
  85. }
  86. </style>