index.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.meta.title }}</span
  9. >
  10. <a v-else @click.prevent="handleLink(item)">{{ item.meta.title }}</a>
  11. </el-breadcrumb-item>
  12. </transition-group>
  13. </el-breadcrumb>
  14. </template>
  15. <script>
  16. import pathToRegexp from "path-to-regexp";
  17. export default {
  18. data() {
  19. return {
  20. levelList: null,
  21. };
  22. },
  23. watch: {
  24. $route(route) {
  25. // if you go to the redirect page, do not update the breadcrumbs
  26. if (route.path.startsWith("/redirect/")) {
  27. return;
  28. }
  29. this.getBreadcrumb();
  30. },
  31. },
  32. created() {
  33. this.getBreadcrumb();
  34. },
  35. methods: {
  36. getBreadcrumb() {
  37. // only show routes with meta.title
  38. let matched = this.$route.matched.filter(
  39. (item) => item.meta && item.meta.title
  40. );
  41. const first = matched[0];
  42. if (!this.isDashboard(first)) {
  43. matched = [{ path: "/index", meta: { title: "首页" } }].concat(matched);
  44. }
  45. this.levelList = matched.filter(
  46. (item) => item.meta && item.meta.title && item.meta.breadcrumb !== false
  47. );
  48. },
  49. isDashboard(route) {
  50. const name = route && route.name;
  51. if (!name) {
  52. return false;
  53. }
  54. return name.trim() === "首页";
  55. },
  56. pathCompile(path) {
  57. const { params } = this.$route;
  58. var toPath = pathToRegexp.compile(path);
  59. return toPath(params);
  60. },
  61. handleLink(item) {
  62. const { redirect, path } = item;
  63. if (redirect) {
  64. this.$router.push(redirect);
  65. return;
  66. }
  67. this.$router.push(this.pathCompile(path));
  68. },
  69. },
  70. };
  71. </script>
  72. <style lang="scss" scoped>
  73. .app-breadcrumb.el-breadcrumb {
  74. display: inline-block;
  75. font-size: 14px;
  76. line-height: 50px;
  77. margin-left: 8px;
  78. .el-breadcrumb__inner.is-link,
  79. .el-breadcrumb__inner a {
  80. color: #fff;
  81. }
  82. .no-redirect {
  83. color: #fff;
  84. cursor: text;
  85. }
  86. }
  87. </style>