SidebarItem.vue 3.2 KB

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