main.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <template>
  2. <div class="Breadcrumb">
  3. <i
  4. class="el-icon-d-arrow-left fl"
  5. :class="{ cor: isShow }"
  6. @click="back_arrow"
  7. ></i>
  8. <el-breadcrumb class="app-breadcrumb" separator="/">
  9. <transition-group name="breadcrumb">
  10. <el-breadcrumb-item v-for="(item, index) in levelList" :key="item.path">
  11. <span
  12. v-if="item.redirect === 'noRedirect' || index > 0"
  13. class="no-redirect"
  14. >{{ item.meta.title }}</span
  15. >
  16. <a v-else @click.prevent="handleLink(item)">{{ item.meta.title }}</a>
  17. </el-breadcrumb-item>
  18. </transition-group>
  19. </el-breadcrumb>
  20. </div>
  21. </template>
  22. <script>
  23. import pathToRegexp from "path-to-regexp";
  24. export default {
  25. name: "Breadcrumb",
  26. data() {
  27. return {
  28. levelList: null,
  29. isShow: false,
  30. backOtions: null,
  31. backUrl: "",
  32. };
  33. },
  34. watch: {
  35. $route(route) {
  36. // if you go to the redirect page, do not update the breadcrumbs
  37. if (route.path.startsWith("/redirect/")) {
  38. this.isShow = false;
  39. return;
  40. }
  41. this.getBreadcrumb();
  42. },
  43. },
  44. created() {
  45. this.getBreadcrumb();
  46. },
  47. methods: {
  48. getBreadcrumb() {
  49. // only show routes with meta.title
  50. let matched = this.$route.matched.filter(
  51. (item) => item.meta && item.meta.title
  52. );
  53. const first = matched[0];
  54. if (!this.isDashboard(first)) {
  55. matched = [
  56. { path: "/welcome/dashboard", meta: { title: "首页" } },
  57. ].concat(matched);
  58. }
  59. this.levelList = matched.filter(
  60. (item) => item.meta && item.meta.title && item.meta.breadcrumb !== false
  61. );
  62. const { preModel } = this.$route.query;
  63. if (preModel) {
  64. let model = JSON.parse(preModel);
  65. this.isShow = true;
  66. const { options, router } = model;
  67. this.backOtions = options;
  68. this.backUrl = router;
  69. } else {
  70. this.isShow = false;
  71. }
  72. },
  73. isDashboard(route) {
  74. const name = route && route.name;
  75. if (!name) {
  76. return false;
  77. }
  78. return (
  79. name.trim().toLocaleLowerCase() === "Dashboard".toLocaleLowerCase()
  80. );
  81. },
  82. pathCompile(path) {
  83. const { params } = this.$route;
  84. var toPath = pathToRegexp.compile(path);
  85. return toPath(params);
  86. },
  87. back_arrow() {
  88. if (!this.isShow) {
  89. return;
  90. }
  91. window.vm.$router.push({
  92. path: this.backUrl,
  93. query: {
  94. back: JSON.stringify(this.backOtions),
  95. },
  96. });
  97. },
  98. handleLink(item) {
  99. const { redirect, path } = item;
  100. if (redirect) {
  101. window.vm.$router.push(redirect);
  102. return;
  103. }
  104. window.vm.$router.push(this.pathCompile(path));
  105. },
  106. },
  107. };
  108. </script>
  109. <style lang="scss" scoped>
  110. .Breadcrumb {
  111. .el-icon-d-arrow-left {
  112. display: inline-block;
  113. font-size: 14px;
  114. line-height: 50px;
  115. margin: 0 8px 0 1px;
  116. color: transparent;
  117. &.cor {
  118. color: #000;
  119. &:hover {
  120. cursor: pointer;
  121. }
  122. }
  123. }
  124. .app-breadcrumb.el-breadcrumb {
  125. display: inline-block;
  126. font-size: 14px;
  127. line-height: 50px;
  128. .no-redirect {
  129. color: #97a8be;
  130. cursor: text;
  131. }
  132. }
  133. }
  134. </style>