index.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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
  6. v-if="item.redirect==='noRedirect'||index==levelList.length-1"
  7. class="no-redirect"
  8. >{{ item.title }}</span>
  9. <a v-else @click.prevent="handleLink(item)">{{ item.title }}</a>
  10. </el-breadcrumb-item>
  11. </transition-group>
  12. </el-breadcrumb>
  13. </template>
  14. <script>
  15. import pathToRegexp from "path-to-regexp";
  16. import menus from "@/components/menu/data";
  17. const menusMap = {};
  18. for (let i = 0, len = menus.length; i < len; i++) {
  19. const menu1 = menus[i];
  20. const subs = menu1.subs;
  21. for (let j = 0, len1 = subs.length; j < len1; j++) {
  22. menusMap[subs[j].path] = [
  23. {
  24. ...menu1
  25. },
  26. {
  27. ...subs[j]
  28. }
  29. ];
  30. }
  31. }
  32. export default {
  33. data() {
  34. return {
  35. levelList: null
  36. };
  37. },
  38. watch: {
  39. $route(route) {
  40. // if you go to the redirect page, do not update the breadcrumbs
  41. if (route.path.startsWith("/redirect/")) {
  42. return;
  43. }
  44. this.getBreadcrumb();
  45. }
  46. },
  47. created() {
  48. this.getBreadcrumb();
  49. },
  50. methods: {
  51. getBreadcrumb() {
  52. // only show routes with meta.title
  53. let matched = this.$route.matched.filter(item => item.name);
  54. const first = matched[0];
  55. if (!this.isDashboard(first)) {
  56. matched = [{ path: "/", title: "Home", redirect: "noRedirect" }].concat(
  57. matched
  58. );
  59. }
  60. this.levelList = [{ path: "/", title: "首页" }].concat(
  61. menusMap[first.path]
  62. );
  63. this.levelList = this.levelList.filter(item => !!item)
  64. console.log(this.levelList);
  65. },
  66. isDashboard(route) {
  67. const name = route && route.name;
  68. if (!name) {
  69. return false;
  70. }
  71. return name.trim().toLocaleLowerCase() === "".toLocaleLowerCase();
  72. },
  73. pathCompile(path) {
  74. // To solve this problem https://github.com/PanJiaChen/vue-element-admin/issues/561
  75. const { params } = this.$route;
  76. var toPath = pathToRegexp.compile(path);
  77. return toPath(params);
  78. },
  79. handleLink(item) {
  80. const { redirect, path } = item;
  81. if (redirect) {
  82. this.$router.push(redirect);
  83. return;
  84. }
  85. this.$router.push(this.pathCompile(path));
  86. }
  87. }
  88. };
  89. </script>
  90. <style lang="scss" scoped>
  91. .app-breadcrumb.el-breadcrumb {
  92. display: inline-block;
  93. font-size: 14px;
  94. line-height: 50px;
  95. margin-left: 8px;
  96. .no-redirect {
  97. color: #97a8be;
  98. cursor: text;
  99. }
  100. }
  101. </style>