SidebarItem.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <template>
  2. <div v-if="!item.hidden" v-show="menuIsShow(item)" class="menu-wrapper">
  3. <!-- &&
  4. !item.alwaysShow && (!onlyOneChild.children|| onlyOneChild.children.length<=1 || onlyOneChild.noShowingChildren)-->
  5. <template
  6. v-if="
  7. hasOneShowingChild(item.children, item)
  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. class="submenu"
  25. ref="subMenu"
  26. :index="resolvePath(item.path)"
  27. popper-append-to-body
  28. >
  29. <template slot="title">
  30. <item
  31. v-if="item.meta"
  32. :icon="item.meta && item.meta.icon"
  33. :title="item.meta.title"
  34. />
  35. </template>
  36. <sidebar-item
  37. v-for="child in item.children"
  38. :key="child.id"
  39. :is-nest="true"
  40. :item="child"
  41. :base-path="resolvePath(child.path)"
  42. class="nest-menu itemIcon"
  43. />
  44. </el-submenu>
  45. </div>
  46. </template>
  47. <script>
  48. import path from "path";
  49. import { isExternal } from "@/utils/validate";
  50. import Item from "./Item";
  51. import AppLink from "./Link";
  52. import FixiOSBug from "./FixiOSBug";
  53. import { getBelongTopMenuPath } from '@/utils/permission';
  54. export default {
  55. name: "SidebarItem",
  56. components: { Item, AppLink },
  57. mixins: [FixiOSBug],
  58. props: {
  59. // route object
  60. item: {
  61. type: Object,
  62. required: true,
  63. },
  64. isNest: {
  65. type: Boolean,
  66. default: false,
  67. },
  68. basePath: {
  69. type: String,
  70. default: "",
  71. },
  72. activeTopMenu: {
  73. type: String,
  74. default: ''
  75. }
  76. },
  77. data() {
  78. // To fix https://github.com/PanJiaChen/vue-admin-template/issues/237
  79. // TODO: refactor with render function
  80. return {};
  81. },
  82. mounted() {
  83. this.onlyOneChild = null;
  84. },
  85. computed: {
  86. getActiveTopMenu: function () {
  87. if (this.activeTopMenu !== '') {
  88. return this.activeTopMenu;
  89. }
  90. const route = this.$route;
  91. /*
  92. const { meta, path } = route;
  93. if (meta.belongTopMenu) {
  94. return meta.belongTopMenu;
  95. }
  96. return '/' + path.split('/')[1];
  97. */
  98. return getBelongTopMenuPath(route);
  99. },
  100. },
  101. methods: {
  102. menuIsShow(route) {
  103. let activeTopMenu = this.basePath;
  104. const { meta } = route;
  105. if (meta.belongTopMenu) {
  106. activeTopMenu = meta.belongTopMenu;
  107. }
  108. //
  109. // (activeTopMenu, this.getActiveTopMenu)
  110. return activeTopMenu === this.getActiveTopMenu;
  111. },
  112. hasOneShowingChild(children = [], parent) {
  113. let count = 0
  114. const showingChildren = children.filter((item) => {
  115. if (item.hidden) {
  116. return false;
  117. } else {
  118. // Temp set(will be used if only has one showing child)
  119. this.onlyOneChild = item;
  120. count++
  121. return true;
  122. }
  123. });
  124. // ('是否又多个子集',showingChildren)
  125. // When there is only one child router, the child router is displayed by default
  126. if (showingChildren.length === 1) {
  127. return true;
  128. }
  129. // Show parent if there are no child router to display
  130. if (showingChildren.length === 0) {
  131. this.onlyOneChild = { ...parent, path: "", noShowingChildren: true };
  132. return true;
  133. }
  134. return false;
  135. },
  136. resolvePath(routePath) {
  137. if (isExternal(routePath)) {
  138. return routePath;
  139. }
  140. if (isExternal(this.basePath)) {
  141. return this.basePath;
  142. }
  143. // debugger
  144. return path.resolve(this.basePath, routePath);
  145. },
  146. },
  147. };
  148. </script>
  149. <style lang="scss" scoped>
  150. // 取消双击选中文字
  151. div {
  152. -moz-user-select: none; /*火狐*/
  153. -webkit-user-select: none; /*webkit浏览器*/
  154. -ms-user-select: none; /*IE10*/
  155. -khtml-user-select: none; /*早期浏览器*/
  156. user-select: none;
  157. }
  158. .itemIcon {
  159. display: flex;
  160. flex-direction: row;
  161. align-items: center;
  162. }
  163. </style>