SidebarItem.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <script setup>
  2. import { computed } from "vue"
  3. import SidebarItemLink from "./SidebarItemLink.vue"
  4. import { isExternal } from "@/utils/validate"
  5. import path from "path-browserify"
  6. const props = defineProps({
  7. item: {
  8. type: Object,
  9. required: true
  10. },
  11. basePath: {
  12. type: String,
  13. default: ""
  14. }
  15. })
  16. /** 是否始终显示根菜单 */
  17. const alwaysShowRootMenu = computed(() => props.item.meta?.alwaysShow)
  18. /** 显示的子菜单 */
  19. const showingChildren = computed(() => {
  20. return props.item.children?.filter((child) => !child.meta?.hidden) ?? []
  21. })
  22. /** 显示的子菜单数量 */
  23. const showingChildNumber = computed(() => {
  24. return showingChildren.value.length
  25. })
  26. /** 唯一的子菜单项 */
  27. const theOnlyOneChild = computed(() => {
  28. const number = showingChildNumber.value
  29. switch (true) {
  30. case number > 1:
  31. return null
  32. case number === 1:
  33. return showingChildren.value[0]
  34. default:
  35. return { ...props.item, path: "" }
  36. }
  37. })
  38. /** 解析路径 */
  39. const resolvePath = (routePath) => {
  40. switch (true) {
  41. case isExternal(routePath):
  42. return routePath
  43. case isExternal(props.basePath):
  44. return props.basePath
  45. default:
  46. return path.resolve(props.basePath, routePath)
  47. }
  48. }
  49. </script>
  50. <template>
  51. <template v-if="!alwaysShowRootMenu && theOnlyOneChild && !theOnlyOneChild.children">
  52. <SidebarItemLink v-if="theOnlyOneChild.meta" :to="resolvePath(theOnlyOneChild.path)">
  53. <el-menu-item :index="resolvePath(theOnlyOneChild.path)">
  54. <SvgIcon v-if="theOnlyOneChild.meta.svgIcon" :name="theOnlyOneChild.meta.svgIcon" />
  55. <component v-else-if="theOnlyOneChild.meta.elIcon" :is="theOnlyOneChild.meta.elIcon" class="el-icon" />
  56. <template v-if="theOnlyOneChild.meta.title" #title>
  57. <span class="title">{{ theOnlyOneChild.meta.title }}</span>
  58. </template>
  59. </el-menu-item>
  60. </SidebarItemLink>
  61. </template>
  62. <el-sub-menu v-else :index="resolvePath(props.item.path)" teleported>
  63. <template #title>
  64. <SvgIcon v-if="props.item.meta?.svgIcon" :name="props.item.meta.svgIcon" />
  65. <component v-else-if="props.item.meta?.elIcon" :is="props.item.meta.elIcon" class="el-icon" />
  66. <span v-if="props.item.meta?.title" class="title">{{ props.item.meta.title }}</span>
  67. </template>
  68. <template v-if="props.item.children">
  69. <SidebarItem
  70. v-for="child in showingChildren"
  71. :key="child.path"
  72. :item="child"
  73. :base-path="resolvePath(child.path)"
  74. />
  75. </template>
  76. </el-sub-menu>
  77. </template>
  78. <style lang="scss" scoped>
  79. .svg-icon {
  80. min-width: 1em;
  81. margin-right: 30px;
  82. font-size: 20px;
  83. z-index: 1;
  84. }
  85. .el-icon {
  86. width: 1em !important;
  87. margin-right: 30px !important;
  88. font-size: 20px;
  89. z-index: 1;
  90. }
  91. .title {
  92. z-index: 1;
  93. }
  94. </style>