index.vue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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(route) {
  21. // if you go to the redirect page, do not update the breadcrumbs
  22. if (route.path.startsWith('/redirect/')) {
  23. return;
  24. }
  25. this.getBreadcrumb();
  26. },
  27. },
  28. created() {
  29. this.getBreadcrumb();
  30. },
  31. methods: {
  32. getBreadcrumb() {
  33. // only show routes with meta.title
  34. let matched = this.$route.matched.filter((item) => item.meta && item.meta.title);
  35. const first = matched[0];
  36. if (!this.isDashboard(first)) {
  37. matched = [{ path: '/home', redirect: '/home', name: 'home', meta: { title: '系统首页' } }].concat(matched);
  38. }
  39. this.levelList = matched.filter((item) => item.meta && item.meta.title && item.meta.breadcrumb !== false);
  40. },
  41. isDashboard(route) {
  42. const name = route && route.name;
  43. if (!name) {
  44. return false;
  45. }
  46. return name.trim().toLocaleLowerCase() === 'home'.toLocaleLowerCase();
  47. },
  48. pathCompile(path) {
  49. // To solve this problem https://github.com/PanJiaChen/vue-element-admin/issues/561
  50. const { params } = this.$route;
  51. var toPath = pathToRegexp.compile(path);
  52. return toPath(params);
  53. },
  54. handleLink(item) {
  55. const { redirect, path } = item;
  56. if (redirect) {
  57. this.$router.push(redirect);
  58. return;
  59. }
  60. this.$router.push(this.pathCompile(path));
  61. },
  62. },
  63. };
  64. </script>
  65. <style lang="less" scoped>
  66. .app-breadcrumb.el-breadcrumb {
  67. display: inline-block;
  68. font-size: 14px;
  69. line-height: 50px;
  70. margin-left: 8px;
  71. .no-redirect {
  72. color: #97a8be;
  73. cursor: text;
  74. }
  75. }
  76. </style>