SidebarItem.vue 2.6 KB

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