SidebarItem.vue 3.1 KB

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