SidebarItem.vue 2.5 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. // TODO: refactor with render function
  52. this.onlyOneChild = null
  53. return {}
  54. },
  55. methods: {
  56. hasOneShowingChild(children = [], parent) {
  57. const showingChildren = children.filter(item => {
  58. if (item.hidden) {
  59. return false
  60. } else {
  61. // Temp set(will be used if only has one showing child)
  62. this.onlyOneChild = item
  63. return true
  64. }
  65. })
  66. // When there is only one child router, the child router is displayed by default
  67. if (showingChildren.length === 1) {
  68. return true
  69. }
  70. // Show parent if there are no child router to display
  71. if (showingChildren.length === 0) {
  72. this.onlyOneChild = { ... parent, path: '', noShowingChildren: true }
  73. return true
  74. }
  75. return false
  76. },
  77. resolvePath(routePath) {
  78. if (isExternal(routePath)) {
  79. return routePath
  80. }
  81. if (isExternal(this.basePath)) {
  82. return this.basePath
  83. }
  84. return path.resolve(this.basePath, routePath)
  85. }
  86. }
  87. }
  88. </script>