SidebarItem.vue 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <template>
  2. <div v-if="!item.hidden&&item.children" class="menu-wrapper">
  3. <template v-if="hasOneShowingChild(item.children) && !onlyOneChild.children&&!item.alwaysShow">
  4. <a :href="onlyOneChild.path" target="_blank" @click="clickLink(onlyOneChild.path,$event)">
  5. <el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown':!isNest}">
  6. <item v-if="onlyOneChild.meta" :icon="onlyOneChild.meta.icon||item.meta.icon" :title="onlyOneChild.meta.title" />
  7. </el-menu-item>
  8. </a>
  9. </template>
  10. <el-submenu v-else :index="item.name||item.path">
  11. <template slot="title">
  12. <item v-if="item.meta" :icon="item.meta.icon" :title="item.meta.title" />
  13. </template>
  14. <template v-for="child in item.children" v-if="!child.hidden">
  15. <sidebar-item
  16. v-if="child.children&&child.children.length>0"
  17. :is-nest="true"
  18. :item="child"
  19. :key="child.path"
  20. :base-path="resolvePath(child.path)"
  21. class="nest-menu"/>
  22. <a v-else :href="child.path" :key="child.name" target="_blank" @click="clickLink(child.path,$event)">
  23. <el-menu-item :index="resolvePath(child.path)">
  24. <item v-if="child.meta" :icon="child.meta.icon" :title="child.meta.title" />
  25. </el-menu-item>
  26. </a>
  27. </template>
  28. </el-submenu>
  29. </div>
  30. </template>
  31. <script>
  32. import path from 'path'
  33. import { validateURL } from '@/utils/validate'
  34. import Item from './Item'
  35. export default {
  36. name: 'SidebarItem',
  37. components: { Item },
  38. props: {
  39. // route配置json
  40. item: {
  41. type: Object,
  42. required: true
  43. },
  44. isNest: {
  45. type: Boolean,
  46. default: false
  47. },
  48. basePath: {
  49. type: String,
  50. default: ''
  51. }
  52. },
  53. data() {
  54. return {
  55. onlyOneChild: null
  56. }
  57. },
  58. methods: {
  59. hasOneShowingChild(children) {
  60. const showingChildren = children.filter(item => {
  61. if (item.hidden) {
  62. return false
  63. } else {
  64. // temp set(will be used if only has one showing child )
  65. this.onlyOneChild = item
  66. return true
  67. }
  68. })
  69. if (showingChildren.length === 1) {
  70. return true
  71. }
  72. return false
  73. },
  74. resolvePath(routePath) {
  75. return path.resolve(this.basePath, routePath)
  76. },
  77. isExternalLink(routePath) {
  78. return validateURL(routePath)
  79. },
  80. clickLink(routePath, e) {
  81. if (!this.isExternalLink(routePath)) {
  82. e.preventDefault()
  83. const path = this.resolvePath(routePath)
  84. this.$router.push(path)
  85. }
  86. }
  87. }
  88. }
  89. </script>