Plat Admin

SidebarItem.vue 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <template>
  2. <div v-if="!item.hidden&&item.children" class="menu-wrapper">
  3. <router-link v-if="hasOneShowingChild(item.children) && !onlyOneChild.children&&!item.alwaysShow" :to="resolvePath(onlyOneChild.path)">
  4. <el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown':!isNest}">
  5. <svg-icon v-if="onlyOneChild.meta&&onlyOneChild.meta.icon" :icon-class="onlyOneChild.meta.icon"></svg-icon>
  6. <span v-if="onlyOneChild.meta&&onlyOneChild.meta.title" slot="title">{{onlyOneChild.meta.title}}</span>
  7. </el-menu-item>
  8. </router-link>
  9. <el-submenu v-else :index="item.name||item.path">
  10. <template slot="title">
  11. <svg-icon v-if="item.meta&&item.meta.icon" :icon-class="item.meta.icon"></svg-icon>
  12. <span v-if="item.meta&&item.meta.title" slot="title">{{item.meta.title}}</span>
  13. </template>
  14. <template v-for="child in item.children" v-if="!child.hidden">
  15. <!-- <div v-if="child.children&&child.children.length>0">
  16. <sidebar-item v-if="!child.parentPath" :is-nest="true" class="nest-menu" :item="child" :key="child.path" :base-path="resolvePath(child.path)"></sidebar-item>
  17. <router-link v-else :to="resolvePath(child.path)"> -->
  18. <sidebar-item :is-nest="true" class="nest-menu" v-if="child.children&&child.children.length>0" :item="child" :key="child.path" :base-path="resolvePath(child.path)" ></sidebar-item>
  19. <!-- </router-link>
  20. </div> -->
  21. <router-link v-else :to="resolvePath(child.path)" :key="child.name">
  22. <el-menu-item :index="resolvePath(child.path)">
  23. <svg-icon v-if="child.meta&&child.meta.icon" :icon-class="child.meta.icon"></svg-icon>
  24. <span v-if="child.meta&&child.meta.title" slot="title">{{child.meta.title}}</span>
  25. </el-menu-item>
  26. </router-link>
  27. </template>
  28. </el-submenu>
  29. </div>
  30. </template>
  31. <script>
  32. import path from 'path'
  33. export default {
  34. name: 'SidebarItem',
  35. props: {
  36. // route配置json
  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. return {
  52. onlyOneChild: null
  53. }
  54. },
  55. methods: {
  56. hasOneShowingChild(children) {
  57. const showingChildren = children.filter(item => {
  58. if (item.hidden) {
  59. return false
  60. } else {
  61. // temp set(will be used if only has one showing child )
  62. this.onlyOneChild = item
  63. return true
  64. }
  65. })
  66. if (showingChildren.length === 1) {
  67. return true
  68. }
  69. return false
  70. },
  71. resolvePath(...paths) {
  72. return path.resolve(this.basePath, ...paths)
  73. }
  74. }
  75. }
  76. </script>