SidebarItem.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <template>
  2. <div v-if="!item.hidden">
  3. <!-- <template v-if="hasOneShowingChild(item.children,item) && (!onlyOneChild.children||onlyOneChild.noShowingChildren) && !item.alwaysShow"> -->
  4. <template v-if="hasOneShowingChild(item.children,item) && (!onlyOneChild.children||onlyOneChild.noShowingChildren || (onlyOneChild.children && onlyOneChild.children.length < 1 && (onlyOneChild.name == 'issue_index' || onlyOneChild.name == 'notice_index' || onlyOneChild.name == 'dashboard'))) && !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. <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. </template>
  15. <sidebar-item
  16. v-for="child in item.children"
  17. :key="child.path"
  18. :is-nest="true"
  19. :item="child"
  20. :base-path="resolvePath(child.path)"
  21. class="nest-menu"
  22. />
  23. </el-submenu>
  24. </div>
  25. </template>
  26. <script>
  27. import path from 'path'
  28. import { isExternal } from '@/utils/validate'
  29. import Item from './Item'
  30. import AppLink from './Link'
  31. import FixiOSBug from './FixiOSBug'
  32. export default {
  33. name: 'SidebarItem',
  34. components: { Item, AppLink },
  35. mixins: [FixiOSBug],
  36. props: {
  37. // route object
  38. item: {
  39. type: Object,
  40. required: true
  41. },
  42. isNest: {
  43. type: Boolean,
  44. default: false
  45. },
  46. basePath: {
  47. type: String,
  48. default: ''
  49. }
  50. },
  51. data() {
  52. // To fix https://github.com/PanJiaChen/vue-admin-template/issues/237
  53. // TODO: refactor with render function
  54. this.onlyOneChild = null
  55. return {}
  56. },
  57. methods: {
  58. hasOneShowingChild(children = [], parent) {
  59. const showingChildren = children.filter(item => {
  60. if (item.hidden) {
  61. return false
  62. } else {
  63. // Temp set(will be used if only has one showing child)
  64. // 删除空的children
  65. if(item.children.length < 1) {
  66. // delete item['children']
  67. }
  68. this.onlyOneChild = item
  69. // console.log(item);
  70. return true
  71. }
  72. })
  73. // When there is only one child router, the child router is displayed by default
  74. if (showingChildren.length === 1) {
  75. return true
  76. }
  77. // Show parent if there are no child router to display
  78. if (showingChildren.length === 0) {
  79. this.onlyOneChild = { ... parent, path: '', noShowingChildren: true }
  80. return true
  81. }
  82. return false
  83. },
  84. resolvePath(routePath) {
  85. if (isExternal(routePath)) {
  86. return routePath
  87. }
  88. if (isExternal(this.basePath)) {
  89. return this.basePath
  90. }
  91. return path.resolve(this.basePath, routePath)
  92. }
  93. }
  94. }
  95. </script>