SidebarItem.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <template>
  2. <div v-if="!item.hidden">
  3. <template v-if="hasOneShowingChild(item.children, item) && (!onlyOneChild.children || onlyOneChild.noShowingChildren) && !item.alwaysShow">
  4. <app-link v-if="onlyOneChild.meta" :to="resolvePath(onlyOneChild.path, onlyOneChild.query)">
  5. <el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{ 'submenu-title-noDropdown': !isNest }">
  6. <svg-icon :icon-class="onlyOneChild.meta.icon || (item.meta && item.meta.icon)"/>
  7. <template #title><span class="menu-title" :title="hasTitle(onlyOneChild.meta.title)">{{ onlyOneChild.meta.title }}</span></template>
  8. </el-menu-item>
  9. </app-link>
  10. </template>
  11. <el-sub-menu v-else ref="subMenu" :index="resolvePath(item.path)" popper-append-to-body>
  12. <template v-if="item.meta" #title>
  13. <svg-icon :icon-class="item.meta && item.meta.icon" />
  14. <span class="menu-title" :title="hasTitle(item.meta.title)">{{ item.meta.title }}</span>
  15. </template>
  16. <sidebar-item
  17. v-for="child in item.children"
  18. :key="child.path"
  19. :is-nest="true"
  20. :item="child"
  21. :base-path="resolvePath(child.path)"
  22. class="nest-menu"
  23. />
  24. </el-sub-menu>
  25. </div>
  26. </template>
  27. <script setup>
  28. import { isExternal } from '@/utils/validate'
  29. import AppLink from './Link'
  30. import { getNormalPath } from '@/utils/ruoyi'
  31. const props = defineProps({
  32. // route object
  33. item: {
  34. type: Object,
  35. required: true
  36. },
  37. isNest: {
  38. type: Boolean,
  39. default: false
  40. },
  41. basePath: {
  42. type: String,
  43. default: ''
  44. }
  45. })
  46. const onlyOneChild = ref({});
  47. function hasOneShowingChild(children = [], parent) {
  48. if (!children) {
  49. children = [];
  50. }
  51. const showingChildren = children.filter(item => {
  52. if (item.hidden) {
  53. return false
  54. } else {
  55. // Temp set(will be used if only has one showing child)
  56. onlyOneChild.value = item
  57. return true
  58. }
  59. })
  60. // When there is only one child router, the child router is displayed by default
  61. if (showingChildren.length === 1) {
  62. return true
  63. }
  64. // Show parent if there are no child router to display
  65. if (showingChildren.length === 0) {
  66. onlyOneChild.value = { ...parent, path: '', noShowingChildren: true }
  67. return true
  68. }
  69. return false
  70. };
  71. function resolvePath(routePath, routeQuery) {
  72. if (isExternal(routePath)) {
  73. return routePath
  74. }
  75. if (isExternal(props.basePath)) {
  76. return props.basePath
  77. }
  78. if (routeQuery) {
  79. let query = JSON.parse(routeQuery);
  80. return { path: getNormalPath(props.basePath + '/' + routePath), query: query }
  81. }
  82. return getNormalPath(props.basePath + '/' + routePath)
  83. }
  84. function hasTitle(title){
  85. if (title.length > 5) {
  86. return title;
  87. } else {
  88. return "";
  89. }
  90. }
  91. </script>