SidebarItem.vue 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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)">
  5. <el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown':!isNest}">
  6. <!-- <item :title="onlyOneChild.meta.title" /> -->
  7. <item :icon="onlyOneChild.meta.icon||(item.meta&&item.meta.icon)" :title="onlyOneChild.meta.title" />
  8. </el-menu-item>
  9. </app-link>
  10. </template>
  11. <el-submenu v-else ref="subMenu" :index="resolvePath(item.path)" popper-append-to-body >
  12. <template slot="title">
  13. <item v-if="item.meta" :icon="item.meta && item.meta.icon" :title="item.meta.title" />
  14. <!-- <item v-if="item.meta" :title="item.meta.title" /> -->
  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-submenu>
  25. </div>
  26. </template>
  27. <script>
  28. import path from 'path'
  29. import { isExternal } from '@/utils/validate'
  30. import Item from './Item'
  31. import AppLink from './Link'
  32. import FixiOSBug from './FixiOSBug'
  33. export default {
  34. name: 'SidebarItem',
  35. components: { Item, AppLink },
  36. mixins: [FixiOSBug],
  37. props: {
  38. // route object
  39. item: {
  40. type: Object,
  41. required: true
  42. },
  43. isNest: {
  44. type: Boolean,
  45. default: false
  46. },
  47. basePath: {
  48. type: String,
  49. default: ''
  50. }
  51. },
  52. data() {
  53. this.onlyOneChild = null
  54. return {
  55. }
  56. },
  57. mounted() {
  58. },
  59. methods: {
  60. hasOneShowingChild(children = [], parent) {
  61. const showingChildren = children.filter(item => {
  62. if (item.hidden) {
  63. return false
  64. } else {
  65. // Temp set(will be used if only has one showing child)
  66. this.onlyOneChild = item
  67. return true
  68. }
  69. })
  70. // When there is only one child router, the child router is displayed by default
  71. if (showingChildren.length === 1) {
  72. return true
  73. }
  74. // Show parent if there are no child router to display
  75. if (showingChildren.length === 0) {
  76. this.onlyOneChild = { ... parent, path: '', noShowingChildren: true }
  77. return true
  78. }
  79. return false
  80. },
  81. resolvePath(routePath) {
  82. if (isExternal(routePath)) {
  83. return routePath
  84. }
  85. if (isExternal(this.basePath)) {
  86. return this.basePath
  87. }
  88. return path.resolve(this.basePath, routePath)
  89. }
  90. }
  91. }
  92. </script>