index.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. router.children[item].path = router.path + "/" + router.children[item].path;
  74. }
  75. router.children[item].parentPath = router.path;
  76. }
  77. childrenMenus.push(router.children[item]);
  78. }
  79. });
  80. return constantRoutes.concat(childrenMenus);
  81. },
  82. // 默认激活的菜单
  83. activeMenu() {
  84. const path = this.$route.path;
  85. let activePath = this.routers[0].path;
  86. if (path.lastIndexOf("/") > 0) {
  87. const tmpPath = path.substring(1, path.length);
  88. activePath = "/" + tmpPath.substring(0, tmpPath.indexOf("/"));
  89. } else if ("/index" == path || "" == path) {
  90. if (!this.isFrist) {
  91. this.isFrist = true;
  92. } else {
  93. activePath = "index";
  94. }
  95. }
  96. var routes = this.activeRoutes(activePath);
  97. if (routes.length === 0) {
  98. activePath = this.currentIndex || this.routers[0].path
  99. this.activeRoutes(activePath);
  100. }
  101. return activePath;
  102. },
  103. },
  104. beforeMount() {
  105. window.addEventListener('resize', this.setVisibleNumber)
  106. },
  107. beforeDestroy() {
  108. window.removeEventListener('resize', this.setVisibleNumber)
  109. },
  110. mounted() {
  111. this.setVisibleNumber();
  112. },
  113. methods: {
  114. // 根据宽度计算设置显示栏数
  115. setVisibleNumber() {
  116. const width = document.body.getBoundingClientRect().width / 3;
  117. this.visibleNumber = parseInt(width / 85);
  118. },
  119. // 菜单选择事件
  120. handleSelect(key, keyPath) {
  121. this.currentIndex = key;
  122. if (key.indexOf("http://") !== -1 || key.indexOf("https://") !== -1) {
  123. // http(s):// 路径新窗口打开
  124. window.open(key, "_blank");
  125. } else if (key.indexOf("/redirect") !== -1) {
  126. // /redirect 路径内部打开
  127. this.$router.push({ path: key.replace("/redirect", "") });
  128. } else {
  129. // 显示左侧联动菜单
  130. this.activeRoutes(key);
  131. }
  132. },
  133. // 当前激活的路由
  134. activeRoutes(key) {
  135. var routes = [];
  136. if (this.childrenMenus && this.childrenMenus.length > 0) {
  137. this.childrenMenus.map((item) => {
  138. if (key == item.parentPath || (key == "index" && "" == item.path)) {
  139. routes.push(item);
  140. }
  141. });
  142. }
  143. if(routes.length > 0) {
  144. this.$store.commit("SET_SIDEBAR_ROUTERS", routes);
  145. }
  146. return routes;
  147. }
  148. },
  149. };
  150. </script>
  151. <style lang="scss">
  152. .el-menu--horizontal > .el-menu-item {
  153. float: left;
  154. height: 50px;
  155. line-height: 50px;
  156. margin: 0;
  157. border-bottom: 3px solid transparent;
  158. color: #999093;
  159. padding: 0 5px;
  160. margin: 0 10px;
  161. }
  162. .el-menu--horizontal > .el-menu-item.is-active {
  163. border-bottom: 3px solid #{'var(--theme)'};
  164. color: #303133;
  165. }
  166. /* submenu item */
  167. .el-menu--horizontal > .el-submenu .el-submenu__title {
  168. height: 50px !important;
  169. line-height: 50px !important;
  170. }
  171. </style>