SidebarItem.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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, onlyOneChild.query)">
  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. this.onlyOneChild = null
  52. return {}
  53. },
  54. methods: {
  55. hasOneShowingChild(children = [], parent) {
  56. if (!children) {
  57. children = [];
  58. }
  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, routeQuery) {
  80. if (isExternal(routePath)) {
  81. return routePath
  82. }
  83. if (isExternal(this.basePath)) {
  84. return this.basePath
  85. }
  86. if (routeQuery) {
  87. let query = JSON.parse(routeQuery);
  88. return { path: path.resolve(this.basePath, routePath), query: query }
  89. }
  90. return path.resolve(this.basePath, routePath)
  91. }
  92. }
  93. }
  94. </script>