SidebarItem.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <div v-if="!item.hidden" class="menu-wrapper">
  3. <template
  4. v-if="
  5. hasOneShowingChild(item.children, item) &&
  6. (!onlyOneChild.children || onlyOneChild.noShowingChildren) &&
  7. !item.alwaysShow
  8. "
  9. >
  10. <app-link v-if="onlyOneChild.meta" :to="resolvePath(onlyOneChild.path)">
  11. <el-menu-item
  12. :index="resolvePath(onlyOneChild.path)"
  13. :class="{ 'submenu-title-noDropdown': !isNest }"
  14. >
  15. <item
  16. :icon="onlyOneChild.meta.icon || (item.meta && item.meta.icon)"
  17. :title="onlyOneChild.meta.title"
  18. />
  19. </el-menu-item>
  20. </app-link>
  21. </template>
  22. <el-submenu
  23. v-else
  24. ref="subMenu"
  25. :index="resolvePath(item.path)"
  26. popper-append-to-body
  27. >
  28. <template slot="title">
  29. <item
  30. v-if="item.meta"
  31. :icon="item.meta && item.meta.icon"
  32. :title="item.meta.title"
  33. />
  34. </template>
  35. <sidebar-item
  36. v-for="child in item.children"
  37. :key="child.id"
  38. :is-nest="true"
  39. :item="child"
  40. :base-path="resolvePath(child.path)"
  41. class="nest-menu itemIcon"
  42. />
  43. </el-submenu>
  44. </div>
  45. </template>
  46. <script>
  47. import path from "path";
  48. import { isExternal } from "@/utils/validate";
  49. import Item from "./Item";
  50. import AppLink from "./Link";
  51. import FixiOSBug from "./FixiOSBug";
  52. export default {
  53. name: "SidebarItem",
  54. components: { Item, AppLink },
  55. mixins: [FixiOSBug],
  56. props: {
  57. // route object
  58. item: {
  59. type: Object,
  60. required: true,
  61. },
  62. isNest: {
  63. type: Boolean,
  64. default: false,
  65. },
  66. basePath: {
  67. type: String,
  68. default: "",
  69. },
  70. },
  71. data() {
  72. // To fix https://github.com/PanJiaChen/vue-admin-template/issues/237
  73. // TODO: refactor with render function
  74. return {};
  75. },
  76. mounted() {
  77. this.onlyOneChild = null;
  78. },
  79. methods: {
  80. hasOneShowingChild(children = [], parent) {
  81. const showingChildren = children.filter((item) => {
  82. if (item.hidden) {
  83. return false;
  84. } else {
  85. // Temp set(will be used if only has one showing child)
  86. this.onlyOneChild = item;
  87. return true;
  88. }
  89. });
  90. // When there is only one child router, the child router is displayed by default
  91. if (showingChildren.length === 1) {
  92. return true;
  93. }
  94. // Show parent if there are no child router to display
  95. if (showingChildren.length === 0) {
  96. this.onlyOneChild = { ...parent, path: "", noShowingChildren: true };
  97. return true;
  98. }
  99. return false;
  100. },
  101. resolvePath(routePath) {
  102. if (isExternal(routePath)) {
  103. return routePath;
  104. }
  105. if (isExternal(this.basePath)) {
  106. return this.basePath;
  107. }
  108. // debugger
  109. return path.resolve(this.basePath, routePath);
  110. },
  111. },
  112. };
  113. </script>
  114. <style lang="scss" scoped>
  115. // 取消双击选中文字
  116. div {
  117. -moz-user-select: none; /*火狐*/
  118. -webkit-user-select: none; /*webkit浏览器*/
  119. -ms-user-select: none; /*IE10*/
  120. -khtml-user-select: none; /*早期浏览器*/
  121. user-select: none;
  122. }
  123. .itemIcon {
  124. display: flex;
  125. flex-direction: row;
  126. align-items: center;
  127. }
  128. </style>