SidebarItem.vue 2.3 KB

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