index.vue 5.3 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 :icon-class="item.meta.icon" />
  10. {{ item.meta.title }}</el-menu-item
  11. >
  12. </template>
  13. <!-- 顶部菜单超出数量折叠 -->
  14. <el-submenu 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. export default {
  31. data() {
  32. return {
  33. // 顶部栏初始数
  34. visibleNumber: 5,
  35. // 是否为首次加载
  36. isFrist: false,
  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 = "/redirect/" + 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 = this.defaultRouter();
  88. if (path.lastIndexOf("/") > 0) {
  89. const tmpPath = path.substring(1, path.length);
  90. activePath = "/" + tmpPath.substring(0, tmpPath.indexOf("/"));
  91. } else if ("/index" == path || "" == path) {
  92. if (!this.isFrist) {
  93. this.isFrist = true;
  94. } else {
  95. activePath = "index";
  96. }
  97. }
  98. var routes = this.activeRoutes(activePath);
  99. if (routes.length === 0) {
  100. activePath = this.currentIndex || this.defaultRouter()
  101. this.activeRoutes(activePath);
  102. }
  103. return activePath;
  104. },
  105. },
  106. beforeMount() {
  107. window.addEventListener('resize', this.setVisibleNumber)
  108. },
  109. beforeDestroy() {
  110. window.removeEventListener('resize', this.setVisibleNumber)
  111. },
  112. mounted() {
  113. this.setVisibleNumber();
  114. },
  115. methods: {
  116. // 根据宽度计算设置显示栏数
  117. setVisibleNumber() {
  118. const width = document.body.getBoundingClientRect().width / 3;
  119. this.visibleNumber = parseInt(width / 85);
  120. },
  121. // 默认激活的路由
  122. defaultRouter() {
  123. let router;
  124. Object.keys(this.routers).some((key) => {
  125. if (!this.routers[key].hidden) {
  126. router = this.routers[key].path;
  127. return true;
  128. }
  129. });
  130. return router;
  131. },
  132. // 菜单选择事件
  133. handleSelect(key, keyPath) {
  134. this.currentIndex = key;
  135. if (this.ishttp(key)) {
  136. // http(s):// 路径新窗口打开
  137. window.open(key, "_blank");
  138. } else if (key.indexOf("/redirect") !== -1) {
  139. // /redirect 路径内部打开
  140. this.$router.push({ path: key.replace("/redirect", "") });
  141. } else {
  142. // 显示左侧联动菜单
  143. this.activeRoutes(key);
  144. }
  145. },
  146. // 当前激活的路由
  147. activeRoutes(key) {
  148. var routes = [];
  149. if (this.childrenMenus && this.childrenMenus.length > 0) {
  150. this.childrenMenus.map((item) => {
  151. if (key == item.parentPath || (key == "index" && "" == item.path)) {
  152. routes.push(item);
  153. }
  154. });
  155. }
  156. if(routes.length > 0) {
  157. this.$store.commit("SET_SIDEBAR_ROUTERS", routes);
  158. }
  159. return routes;
  160. },
  161. ishttp(url) {
  162. return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1
  163. }
  164. },
  165. };
  166. </script>
  167. <style lang="scss">
  168. .el-menu--horizontal > .el-menu-item {
  169. float: left;
  170. height: 50px;
  171. line-height: 50px;
  172. margin: 0;
  173. border-bottom: 3px solid transparent;
  174. color: #999093;
  175. padding: 0 5px;
  176. margin: 0 10px;
  177. }
  178. .el-menu--horizontal > .el-menu-item.is-active {
  179. border-bottom: 3px solid #{'var(--theme)'};
  180. color: #303133;
  181. }
  182. /* submenu item */
  183. .el-menu--horizontal > .el-submenu .el-submenu__title {
  184. height: 50px !important;
  185. line-height: 50px !important;
  186. }
  187. </style>