SidebarItem.vue 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <template>
  2. <div v-if="!item.hidden && item.children" class="menu-wrapper">
  3. <div v-if="item.other">
  4. <a :href="item.src" target="_blank">
  5. <el-menu-item
  6. :index="resolvePath(item.src)"
  7. :class="{ 'submenu-title-noDropdown': !isNest }"
  8. >
  9. <i
  10. v-if="item.name == 'slow'"
  11. class="icon iconfont icon-manbing-xuanzhong"
  12. style="margin-right: 4px;"
  13. ></i>
  14. <i
  15. v-if="item.name == 'scrm'"
  16. class="icon iconfont icon-pengyou"
  17. style="margin-right: 4px;"
  18. ></i>
  19. <i
  20. v-if="item.name == 'shop'"
  21. class="icon iconfont icon-service_fill"
  22. style="margin-right: 4px;"
  23. ></i>
  24. <span slot="title">{{ generateTitle(item.name) }}</span>
  25. </el-menu-item>
  26. </a>
  27. </div>
  28. <div v-else>
  29. <div
  30. v-if="
  31. hasOneShowingChild(item.children) &&
  32. !onlyOneChild.children &&
  33. !item.alwaysShow
  34. "
  35. @click="onClick(item)"
  36. >
  37. <router-link :to="resolvePath(onlyOneChild.path)">
  38. <el-menu-item
  39. :index="resolvePath(onlyOneChild.path)"
  40. :class="{ 'submenu-title-noDropdown': !isNest }"
  41. >
  42. <svg-icon
  43. v-if="onlyOneChild.meta && onlyOneChild.meta.icon"
  44. :icon-class="onlyOneChild.meta.icon"
  45. ></svg-icon>
  46. <span
  47. v-if="onlyOneChild.meta && onlyOneChild.meta.title"
  48. slot="title"
  49. >{{ generateTitle(onlyOneChild.meta.title) }}</span
  50. >
  51. </el-menu-item>
  52. </router-link>
  53. </div>
  54. <div v-else>
  55. <el-submenu
  56. v-if="item.meta.isChild"
  57. id="mySubmenu"
  58. :index="item.name || item.path"
  59. >
  60. <template slot="title">
  61. <svg-icon
  62. v-if="item.meta && item.meta.icon"
  63. :icon-class="item.meta.icon"
  64. ></svg-icon>
  65. <span v-if="item.meta && item.meta.title" slot="title">
  66. {{ generateTitle(item.meta.title) }}
  67. </span>
  68. </template>
  69. <template v-for="child in item.children" v-if="!child.hidden">
  70. <router-link
  71. :to="resolvePath(child.path)"
  72. v-if="child.meta.isChild != true"
  73. :key="child.name"
  74. >
  75. <el-menu-item
  76. @click="onClick(item)"
  77. :index="resolvePath(child.path)"
  78. >
  79. <svg-icon
  80. v-if="child.meta && child.meta.icon"
  81. :icon-class="child.meta.icon"
  82. ></svg-icon>
  83. <span v-if="child.meta && child.meta.title" slot="title">
  84. {{ generateTitle(child.meta.title) }}
  85. </span>
  86. </el-menu-item>
  87. </router-link>
  88. <router-link
  89. v-if="child.meta.isChild == true"
  90. :to="child.path"
  91. :key="child.name"
  92. >
  93. <el-menu-item
  94. @click="onClick(item, child.parentNum)"
  95. :index="resolvePath(child.path)"
  96. >
  97. <svg-icon
  98. v-if="child.meta && child.meta.icon"
  99. :icon-class="child.meta.icon"
  100. ></svg-icon>
  101. <span v-if="child.meta && child.meta.title" slot="title">
  102. {{ generateTitle(child.meta.title) }}
  103. </span>
  104. </el-menu-item>
  105. </router-link>
  106. </template>
  107. </el-submenu>
  108. <div v-else @click="onClick(item)">
  109. <router-link :to="resolvePath(item.children[0].path)">
  110. <el-menu-item :index="item.name || item.path">
  111. <template slot="title">
  112. <svg-icon
  113. v-if="item.meta && item.meta.icon"
  114. :icon-class="item.meta.icon"
  115. ></svg-icon>
  116. <span v-if="item.meta && item.meta.title" slot="title">
  117. {{ generateTitle(item.meta.title) }}
  118. </span>
  119. </template>
  120. <!-- <template v-for="child in item.children" v-if="!child.hidden">
  121. <sidebar-item
  122. :is-nest="true"
  123. class="nest-menu"
  124. v-if="child.children && child.children.length > 0"
  125. :item="child"
  126. :key="child.path"
  127. :base-path="resolvePath(child.path)"
  128. ></sidebar-item>
  129. <router-link
  130. v-else
  131. :to="resolvePath(child.path)"
  132. :key="child.name"
  133. >
  134. <el-menu-item :index="resolvePath(child.path)">
  135. <svg-icon
  136. v-if="child.meta && child.meta.icon"
  137. :icon-class="child.meta.icon"
  138. ></svg-icon>
  139. <span v-if="child.meta && child.meta.title" slot="title">{{
  140. generateTitle(child.meta.title)
  141. }}</span>
  142. </el-menu-item>
  143. </router-link>
  144. </template> -->
  145. </el-menu-item>
  146. </router-link>
  147. </div>
  148. </div>
  149. </div>
  150. </div>
  151. </template>
  152. <script>
  153. import path from "path";
  154. import { generateTitle } from "@/utils/i18n";
  155. import { mapGetters } from "vuex";
  156. export default {
  157. name: "SidebarItem",
  158. props: {
  159. // route object
  160. item: {
  161. type: Object,
  162. required: true,
  163. },
  164. isNest: {
  165. type: Boolean,
  166. default: false,
  167. },
  168. basePath: {
  169. type: String,
  170. default: "",
  171. },
  172. index: {
  173. type: Number,
  174. },
  175. },
  176. computed: {
  177. ...mapGetters(["permission_routers"]),
  178. },
  179. data() {
  180. return {
  181. onlyOneChild: null,
  182. };
  183. },
  184. methods: {
  185. hasOneShowingChild(children) {
  186. const showingChildren = children.filter((item) => {
  187. if (item.hidden) {
  188. return false;
  189. } else {
  190. // temp set(will be used if only has one showing child )
  191. this.onlyOneChild = item;
  192. return true;
  193. }
  194. });
  195. if (showingChildren.length === 1) {
  196. return true;
  197. }
  198. return false;
  199. },
  200. resolvePath(...paths) {
  201. return path.resolve(this.basePath, ...paths);
  202. },
  203. generateTitle,
  204. onClick(index, num) {
  205. this.permission_routers.map((item, i) => {
  206. if (this.isObjectValueEqual(item, index)) {
  207. if (num) {
  208. this.$emit("a", i, num);
  209. } else {
  210. this.$emit("a", i, 999);
  211. }
  212. }
  213. });
  214. },
  215. isObjectValueEqual(a, b) {
  216. var aProps = Object.getOwnPropertyNames(a);
  217. var bProps = Object.getOwnPropertyNames(b);
  218. if (aProps.length != bProps.length) {
  219. return false;
  220. }
  221. for (var i = 0; i < aProps.length; i++) {
  222. var propName = aProps[i];
  223. if (a[propName] !== b[propName]) {
  224. return false;
  225. }
  226. }
  227. return true;
  228. },
  229. },
  230. created() {
  231. console.log(this.permission_routers);
  232. },
  233. };
  234. </script>
  235. <style lang="scss">
  236. #mySubmenu {
  237. .el-menu {
  238. display: flex;
  239. flex-wrap: wrap;
  240. > a {
  241. width: 50% !important;
  242. }
  243. .el-menu-item {
  244. min-width: 0;
  245. padding: 0 5px !important;
  246. font-size: 12px;
  247. background-color: #1f2d3d !important;
  248. }
  249. }
  250. }
  251. .menu-wrapper {
  252. .el-menu-item {
  253. background-color: #1f2d3d !important;
  254. color: #fff !important;
  255. }
  256. .el-menu {
  257. background-color: #1f2d3d !important;
  258. }
  259. .el-submenu__title {
  260. background-color: #1f2d3d !important;
  261. color: #fff !important;
  262. height: 32px !important;
  263. line-height: 32px !important;
  264. }
  265. .router-link-exact-active {
  266. color: rgb(64, 158, 255) !important;
  267. .el-menu-item {
  268. color: rgb(64, 158, 255) !important;
  269. }
  270. }
  271. }
  272. </style>