SidebarItem.vue 3.0 KB

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