SidebarItem.vue 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 v-for="child in item.children" :key="child.path" :is-nest="true" :item="child" :base-path="resolvePath(child.path)"
  15. class="nest-menu" />
  16. </el-submenu>
  17. </div>
  18. </template>
  19. <script>
  20. import path from 'path'
  21. import {
  22. isExternal
  23. } from '@/utils/validate'
  24. import Item from './Item'
  25. import AppLink from './Link'
  26. import FixiOSBug from './FixiOSBug'
  27. export default {
  28. name: 'SidebarItem',
  29. components: {
  30. Item,
  31. AppLink
  32. },
  33. mixins: [FixiOSBug],
  34. props: {
  35. // route object
  36. item: {
  37. type: Object,
  38. required: true
  39. },
  40. isNest: {
  41. type: Boolean,
  42. default: false
  43. },
  44. basePath: {
  45. type: String,
  46. default: ''
  47. }
  48. },
  49. data() {
  50. // To fix https://github.com/PanJiaChen/vue-admin-template/issues/237
  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 = {
  73. ...parent,
  74. path: '',
  75. noShowingChildren: true
  76. }
  77. return true
  78. }
  79. return false
  80. },
  81. resolvePath(routePath) {
  82. if (isExternal(routePath)) {
  83. return routePath
  84. }
  85. if (isExternal(this.basePath)) {
  86. return this.basePath
  87. }
  88. return path.resolve(this.basePath, routePath)
  89. }
  90. }
  91. }
  92. </script>