index.vue 5.3 KB

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