SidebarItem.vue 3.0 KB

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