SidebarItem.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <div v-if="!item.hidden">
  3. <!-- 一级菜单 -->
  4. <template v-if="hasOneShowingChild(item.children, item) && (!onlyOneChild.children || onlyOneChild.noShowingChildren) && !item.alwaysShow">
  5. <app-link v-if="onlyOneChild.meta" :to="resolvePath(onlyOneChild.path)">
  6. <el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown': !isNest}">
  7. <!-- 动态显示图标 -->
  8. <img v-if="!isNest && onlyOneChild.meta" :src="getIcon(onlyOneChild)" class="sidebar-icon" />
  9. <span>{{ generateTitle(onlyOneChild.meta.title) }}</span>
  10. </el-menu-item>
  11. </app-link>
  12. </template>
  13. <!-- 子菜单 -->
  14. <el-submenu v-else ref="subMenu" :index="resolvePath(item.path)" popper-append-to-body>
  15. <template slot="title">
  16. <!-- 动态显示图标 -->
  17. <img v-if="!isNest && item.meta" :src="getIcon(item)" class="sidebar-icon" />
  18. <span>{{ generateTitle(item.meta.title) }}</span>
  19. </template>
  20. <!-- 递归渲染子菜单 -->
  21. <sidebar-item
  22. v-for="child in item.children"
  23. :key="child.path"
  24. :is-nest="true"
  25. :item="child"
  26. :base-path="resolvePath(child.path)"
  27. class="nest-menu"
  28. />
  29. </el-submenu>
  30. </div>
  31. </template>
  32. <script>
  33. import path from 'path'
  34. import { isExternal } from '@/utils/validate'
  35. import AppLink from './Link'
  36. import FixiOSBug from './FixiOSBug'
  37. export default {
  38. name: 'SidebarItem',
  39. components: { AppLink },
  40. mixins: [FixiOSBug],
  41. props: {
  42. item: {
  43. type: Object,
  44. required: true
  45. },
  46. isNest: {
  47. type: Boolean,
  48. default: false
  49. },
  50. basePath: {
  51. type: String,
  52. default: ''
  53. }
  54. },
  55. data() {
  56. return {
  57. onlyOneChild: null
  58. }
  59. },
  60. methods: {
  61. hasOneShowingChild(children = [], parent) {
  62. const showingChildren = children.filter(item => !item.hidden)
  63. if (showingChildren.length === 1) {
  64. this.onlyOneChild = showingChildren[0]
  65. return true
  66. }
  67. if (showingChildren.length === 0) {
  68. this.onlyOneChild = { ...parent, path: '', noShowingChildren: true }
  69. return true
  70. }
  71. return false
  72. },
  73. resolvePath(routePath) {
  74. if (isExternal(routePath)) {
  75. return routePath
  76. }
  77. if (isExternal(this.basePath)) {
  78. return this.basePath
  79. }
  80. return path.resolve(this.basePath, routePath)
  81. },
  82. generateTitle(metaTitle) {
  83. if (this.$te(metaTitle)) {
  84. return this.$t(metaTitle)
  85. }
  86. return metaTitle || 'No Title'
  87. },
  88. // 动态获取图标
  89. getIcon(item) {
  90. const currentPath = this.$route.path;
  91. // 直接检查当前路径是否与一级菜单的路径匹配
  92. if (this.resolvePath(item.path) === currentPath) {
  93. return item.meta.selected_icon; // 如果一级菜单本身被选中,返回 selected_icon
  94. }
  95. // 递归检查是否存在子路径匹配当前路径
  96. const isPathInChildren = (children) => {
  97. return children.some(child => {
  98. if (this.resolvePath(child.path) === currentPath) {
  99. return true;
  100. }
  101. return child.children ? isPathInChildren(child.children) : false;
  102. });
  103. };
  104. // 如果当前路径匹配第一级菜单的子路径,返回 selected_icon
  105. if (isPathInChildren(item.children || [])) {
  106. return item.meta.selected_icon; // 返回选中状态的图标
  107. }
  108. return item.meta.icon; // 返回默认图标
  109. },
  110. // 新增方法:获取只有一个子项的图标
  111. getOnlyOneChildIcon() {
  112. if (this.onlyOneChild) {
  113. return this.getIcon(this.onlyOneChild);
  114. }
  115. return ''; // 或者返回一个默认图标
  116. }
  117. }
  118. }
  119. </script>
  120. <style scoped>
  121. .sidebar-icon {
  122. width: 20px;
  123. height: 20px;
  124. margin-right: 12px;
  125. vertical-align: middle;
  126. }
  127. </style>