SidebarItem.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <template>
  2. <div v-if="!item.hidden">
  3. <template v-if="hasOneShowingChild(item.children,item) && (!onlyOneChild.children||onlyOneChild.noShowingChildren)&&!item.alwaysShow">
  4. <div>1111</div>
  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. this.onlyOneChild = item
  65. return true
  66. }
  67. })
  68. // When there is only one child router, the child router is displayed by default
  69. if (showingChildren.length === 1) {
  70. return true
  71. }
  72. // Show parent if there are no child router to display
  73. if (showingChildren.length === 0) {
  74. this.onlyOneChild = { ... parent, path: '', noShowingChildren: true }
  75. return true
  76. }
  77. return false
  78. },
  79. resolvePath(routePath) {
  80. if (isExternal(routePath)) {
  81. return routePath
  82. }
  83. if (isExternal(this.basePath)) {
  84. return this.basePath
  85. }
  86. return path.resolve(this.basePath, routePath)
  87. }
  88. }
  89. }
  90. </script>
  91. <style lang="scss" scoped>
  92. .svg-icon{
  93. width: 1.8em !important;
  94. margin-left: -3px;
  95. margin-right: 10px !important;
  96. }
  97. </style>