breadCrumb.vue 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <template>
  2. <div class="bread-crumb">
  3. <div class="crumb-box">
  4. <div class="crumb-item" style="opacity: 1;cursor: pointer;" @click="$router.push('/work')">
  5. <span>个人工作台</span>
  6. <span class="tips">/</span>
  7. </div>
  8. <div class="crumb-item" v-for="(item,index) in levelList" :key="index">
  9. <span>{{item}}</span>
  10. <span class="tips" v-if="index !== levelList.length - 1">/</span>
  11. </div>
  12. </div>
  13. <div style="display: flex;align-items: center;">
  14. <span>{{$store.getters.organization.name}}</span>
  15. <span class="hui-tag hui-tag-success" style="margin-left: 10px;">{{$store.getters.identity.name}}</span>
  16. </div>
  17. </div>
  18. </template>
  19. <script>
  20. export default {
  21. name: 'breadCrumb',
  22. data() {
  23. return {
  24. levelList: []
  25. }
  26. },
  27. watch: {
  28. $route() {
  29. this.getBreadcrumb();
  30. }
  31. },
  32. created() {
  33. this.menuList = this.$store.getters.menuData;
  34. this.getBreadcrumb();
  35. },
  36. methods: {
  37. getBreadcrumb() {
  38. this.levelList = this.findParent(this.menuList, this.$route.path);
  39. if (this.levelList.length === 0) this.levelList.push(this.$route.name);
  40. },
  41. findParent(dataSource, path) {
  42. const parentIds = []; // 用于存储所有父节点ID的数组
  43. // 定义一个递归函数,用于遍历整棵树并查找子节点的所有父节点
  44. function traverse(node, path) {
  45. if (node.index === path) { // 如果当前节点的ID等于子节点的ID,则表示已经找到了子节点,可以开始向上查找父节点
  46. parentIds.push(node.title);
  47. return true; // 返回true表示已经找到了子节点
  48. }
  49. if (node.children) { // 如果当前节点有子节点,则继续遍历子节点
  50. for (const childNode of node.children) {
  51. if (traverse(childNode, path)) { // 如果在子节点中找到了子节点的父节点,则将当前节点的ID添加到父节点ID数组中,并返回true表示已经找到了子节点
  52. parentIds.unshift(node.title);
  53. return true;
  54. }
  55. }
  56. }
  57. return false; // 如果当前节点不是子节点的父节点,则返回false
  58. }
  59. // 从根节点开始遍历整棵树,并调用递归函数查找子节点的所有父节点
  60. for (const node of dataSource) {
  61. if (traverse(node, path)) { // 如果在当前节点的子树中找到了子节点的父节点,则直接退出循环
  62. break;
  63. }
  64. }
  65. return parentIds; // 返回所有父节点ID的数组
  66. }
  67. }
  68. }
  69. </script>
  70. <style lang="scss">
  71. .bread-crumb {
  72. display: flex;
  73. justify-content: space-between;
  74. .crumb-box {
  75. position: relative;
  76. padding-top: 1px;
  77. display: flex;
  78. align-items: center;
  79. .crumb-item {
  80. opacity: 0.5;
  81. font-weight: 400;
  82. }
  83. .crumb-item:last-child {
  84. opacity: 1;
  85. }
  86. .tips {
  87. margin: 0px 3px;
  88. }
  89. }
  90. }
  91. </style>