SidebarItem.vue 2.6 KB

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