SideBarItem.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <template>
  2. <component
  3. :is="menuComponent"
  4. v-if="!item.hidden"
  5. :item="item"
  6. :full-path="fullPath"
  7. :route-children="routeChildren"
  8. >
  9. <template v-if="item.children && item.children.length">
  10. <side-bar-item
  11. v-for="route in item.children"
  12. :key="route.path"
  13. :full-path="handlePath(route.path)"
  14. :item="route"
  15. ></side-bar-item>
  16. </template>
  17. </component>
  18. </template>
  19. <script>
  20. import Submenu from "./Submenu";
  21. import MenuItem from "./MenuItem";
  22. import { isExternal } from "@/utils/validate";
  23. import path from "path";
  24. export default {
  25. name: "SideBarItem",
  26. components: { Submenu, MenuItem },
  27. props: {
  28. item: {
  29. type: Object,
  30. required: true,
  31. },
  32. fullPath: {
  33. type: String,
  34. default: "",
  35. },
  36. },
  37. data() {
  38. this.onlyOneChild = null;
  39. return {};
  40. },
  41. computed: {
  42. menuComponent() {
  43. if (
  44. this.handleChildren(this.item.children, this.item) &&
  45. (!this.routeChildren.children ||
  46. this.routeChildren.notShowChildren) &&
  47. !this.item.alwaysShow
  48. ) {
  49. return "MenuItem";
  50. } else {
  51. return "Submenu";
  52. }
  53. },
  54. },
  55. methods: {
  56. handleChildren(children = [], parent) {
  57. if (children === null) children = [];
  58. const showChildren = children.filter((item) => {
  59. if (item.hidden) {
  60. return false;
  61. } else {
  62. this.routeChildren = item;
  63. return true;
  64. }
  65. });
  66. if (showChildren.length === 1) {
  67. return true;
  68. }
  69. if (showChildren.length === 0) {
  70. this.routeChildren = {
  71. ...parent,
  72. path: "",
  73. notShowChildren: true,
  74. };
  75. return true;
  76. }
  77. return false;
  78. },
  79. handlePath(routePath) {
  80. if (isExternal(routePath)) {
  81. return routePath;
  82. }
  83. if (isExternal(this.fullPath)) {
  84. return this.fullPath;
  85. }
  86. return path.resolve(this.fullPath, routePath);
  87. },
  88. },
  89. };
  90. </script>
  91. <style lang="scss" scoped>
  92. .vab-nav-icon {
  93. margin-right: 4px;
  94. }
  95. ::v-deep {
  96. .el-tag {
  97. float: right;
  98. height: 16px;
  99. padding-right: 4px;
  100. padding-left: 4px;
  101. margin-top: calc((#{$base-menu-item-height} - 16px) / 2);
  102. line-height: 16px;
  103. border: 0;
  104. }
  105. }
  106. </style>