VabSideBarItem.vue 2.3 KB

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