index.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <template>
  2. <el-menu
  3. :default-active="activeMenu"
  4. mode="horizontal"
  5. @select="handleSelect"
  6. >
  7. <template v-for="(item, index) in topMenus">
  8. <el-menu-item :style="{'--theme': theme}" :index="item.path" :key="index" v-if="index < visibleNumber">
  9. <svg-icon
  10. v-if="item.meta && item.meta.icon && item.meta.icon !== '#'"
  11. :icon-class="item.meta.icon"/>
  12. {{ item.meta.title }}
  13. </el-menu-item>
  14. </template>
  15. <!-- 顶部菜单超出数量折叠 -->
  16. <el-submenu :style="{'--theme': theme}" index="more" v-if="topMenus.length > visibleNumber">
  17. <template slot="title">更多菜单</template>
  18. <template v-for="(item, index) in topMenus">
  19. <el-menu-item
  20. :index="item.path"
  21. :key="index"
  22. v-if="index >= visibleNumber">
  23. <svg-icon
  24. v-if="item.meta && item.meta.icon && item.meta.icon !== '#'"
  25. :icon-class="item.meta.icon"/>
  26. {{ item.meta.title }}
  27. </el-menu-item>
  28. </template>
  29. </el-submenu>
  30. </el-menu>
  31. </template>
  32. <script>
  33. import { constantRoutes } from "@/router";
  34. import { isHttp } from "@/utils/validate";
  35. // 隐藏侧边栏路由
  36. const hideList = ['/index', '/user/profile'];
  37. export default {
  38. data() {
  39. return {
  40. // 顶部栏初始数
  41. visibleNumber: 5,
  42. // 当前激活菜单的 index
  43. currentIndex: undefined
  44. };
  45. },
  46. computed: {
  47. theme() {
  48. return this.$store.state.settings.theme;
  49. },
  50. // 顶部显示菜单
  51. topMenus() {
  52. let topMenus = [];
  53. this.routers.map((menu) => {
  54. if (menu.hidden !== true) {
  55. // 兼容顶部栏一级菜单内部跳转
  56. if (menu.path === "/") {
  57. topMenus.push(menu.children[0]);
  58. } else {
  59. topMenus.push(menu);
  60. }
  61. }
  62. });
  63. return topMenus;
  64. },
  65. // 所有的路由信息
  66. routers() {
  67. return this.$store.state.permission.topbarRouters;
  68. },
  69. // 设置子路由
  70. childrenMenus() {
  71. var childrenMenus = [];
  72. this.routers.map((router) => {
  73. for (var item in router.children) {
  74. if (router.children[item].parentPath === undefined) {
  75. if(router.path === "/") {
  76. router.children[item].path = "/" + router.children[item].path;
  77. } else {
  78. if(!isHttp(router.children[item].path)) {
  79. router.children[item].path = router.path + "/" + router.children[item].path;
  80. }
  81. }
  82. router.children[item].parentPath = router.path;
  83. }
  84. childrenMenus.push(router.children[item]);
  85. }
  86. });
  87. return constantRoutes.concat(childrenMenus);
  88. },
  89. // 默认激活的菜单
  90. activeMenu() {
  91. const path = this.$route.path;
  92. let activePath = path;
  93. if (path !== undefined && path.lastIndexOf("/") > 0 && hideList.indexOf(path) === -1) {
  94. const tmpPath = path.substring(1, path.length);
  95. activePath = "/" + tmpPath.substring(0, tmpPath.indexOf("/"));
  96. if (!this.$route.meta.link) {
  97. this.$store.dispatch('app/toggleSideBarHide', false);
  98. }
  99. } else if(!this.$route.children) {
  100. activePath = path;
  101. this.$store.dispatch('app/toggleSideBarHide', true);
  102. }
  103. this.activeRoutes(activePath);
  104. return activePath;
  105. },
  106. },
  107. beforeMount() {
  108. window.addEventListener('resize', this.setVisibleNumber)
  109. },
  110. beforeDestroy() {
  111. window.removeEventListener('resize', this.setVisibleNumber)
  112. },
  113. mounted() {
  114. this.setVisibleNumber();
  115. },
  116. methods: {
  117. // 根据宽度计算设置显示栏数
  118. setVisibleNumber() {
  119. const width = document.body.getBoundingClientRect().width / 3;
  120. this.visibleNumber = parseInt(width / 85);
  121. },
  122. // 菜单选择事件
  123. handleSelect(key, keyPath) {
  124. this.currentIndex = key;
  125. const route = this.routers.find(item => item.path === key);
  126. if (isHttp(key)) {
  127. // http(s):// 路径新窗口打开
  128. window.open(key, "_blank");
  129. } else if (!route || !route.children) {
  130. // 没有子路由路径内部打开
  131. const routeMenu = this.childrenMenus.find(item => item.path === key);
  132. if (routeMenu && routeMenu.query) {
  133. let query = JSON.parse(routeMenu.query);
  134. this.$router.push({ path: key, query: query });
  135. } else {
  136. this.$router.push({ path: key });
  137. }
  138. this.$store.dispatch('app/toggleSideBarHide', true);
  139. } else {
  140. // 显示左侧联动菜单
  141. this.activeRoutes(key);
  142. this.$store.dispatch('app/toggleSideBarHide', false);
  143. }
  144. },
  145. // 当前激活的路由
  146. activeRoutes(key) {
  147. var routes = [];
  148. if (this.childrenMenus && this.childrenMenus.length > 0) {
  149. this.childrenMenus.map((item) => {
  150. if (key == item.parentPath || (key == "index" && "" == item.path)) {
  151. routes.push(item);
  152. }
  153. });
  154. }
  155. if(routes.length > 0) {
  156. this.$store.commit("SET_SIDEBAR_ROUTERS", routes);
  157. } else {
  158. this.$store.dispatch('app/toggleSideBarHide', true);
  159. }
  160. }
  161. },
  162. };
  163. </script>
  164. <style lang="scss">
  165. .topmenu-container.el-menu--horizontal > .el-menu-item {
  166. float: left;
  167. height: 50px !important;
  168. line-height: 50px !important;
  169. color: #999093 !important;
  170. padding: 0 5px !important;
  171. margin: 0 10px !important;
  172. }
  173. .topmenu-container.el-menu--horizontal > .el-menu-item.is-active, .el-menu--horizontal > .el-submenu.is-active .el-submenu__title {
  174. border-bottom: 2px solid #{'var(--theme)'} !important;
  175. color: #303133;
  176. }
  177. /* submenu item */
  178. .topmenu-container.el-menu--horizontal > .el-submenu .el-submenu__title {
  179. float: left;
  180. height: 50px !important;
  181. line-height: 50px !important;
  182. color: #999093 !important;
  183. padding: 0 5px !important;
  184. margin: 0 10px !important;
  185. }
  186. </style>