index.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 :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. // 顶部显示菜单
  43. topMenus() {
  44. let topMenus = [];
  45. this.routers.map((menu) => {
  46. if (menu.hidden !== true) {
  47. // 兼容顶部栏一级菜单内部跳转
  48. if (menu.path === "/") {
  49. topMenus.push(menu.children[0]);
  50. } else {
  51. topMenus.push(menu);
  52. }
  53. }
  54. });
  55. return topMenus;
  56. },
  57. // 所有的路由信息
  58. routers() {
  59. return this.$store.state.permission.topbarRouters;
  60. },
  61. // 设置子路由
  62. childrenMenus() {
  63. var childrenMenus = [];
  64. this.routers.map((router) => {
  65. for (var item in router.children) {
  66. if (router.children[item].parentPath === undefined) {
  67. if(router.path === "/") {
  68. router.children[item].path = "/redirect/" + router.children[item].path;
  69. } else {
  70. router.children[item].path = router.path + "/" + router.children[item].path;
  71. }
  72. router.children[item].parentPath = router.path;
  73. }
  74. childrenMenus.push(router.children[item]);
  75. }
  76. });
  77. return constantRoutes.concat(childrenMenus);
  78. },
  79. // 默认激活的菜单
  80. activeMenu() {
  81. const path = this.$route.path;
  82. let activePath = this.routers[0].path;
  83. if (path.lastIndexOf("/") > 0) {
  84. const tmpPath = path.substring(1, path.length);
  85. activePath = "/" + tmpPath.substring(0, tmpPath.indexOf("/"));
  86. } else if ("/index" == path || "" == path) {
  87. if (!this.isFrist) {
  88. this.isFrist = true;
  89. } else {
  90. activePath = "index";
  91. }
  92. }
  93. var routes = this.activeRoutes(activePath);
  94. if (routes.length === 0) {
  95. activePath = this.currentIndex || this.routers[0].path
  96. this.activeRoutes(activePath);
  97. }
  98. return activePath;
  99. },
  100. },
  101. beforeMount() {
  102. window.addEventListener('resize', this.setVisibleNumber)
  103. },
  104. beforeDestroy() {
  105. window.removeEventListener('resize', this.setVisibleNumber)
  106. },
  107. mounted() {
  108. this.setVisibleNumber();
  109. },
  110. methods: {
  111. // 根据宽度计算设置显示栏数
  112. setVisibleNumber() {
  113. const width = document.body.getBoundingClientRect().width / 3;
  114. this.visibleNumber = parseInt(width / 85);
  115. },
  116. // 菜单选择事件
  117. handleSelect(key, keyPath) {
  118. this.currentIndex = key;
  119. if (key.indexOf("http://") !== -1 || key.indexOf("https://") !== -1) {
  120. // http(s):// 路径新窗口打开
  121. window.open(key, "_blank");
  122. } else if (key.indexOf("/redirect") !== -1) {
  123. // /redirect 路径内部打开
  124. this.$router.push({ path: key.replace("/redirect", "") });
  125. } else {
  126. // 显示左侧联动菜单
  127. this.activeRoutes(key);
  128. }
  129. },
  130. // 当前激活的路由
  131. activeRoutes(key) {
  132. var routes = [];
  133. if (this.childrenMenus && this.childrenMenus.length > 0) {
  134. this.childrenMenus.map((item) => {
  135. if (key == item.parentPath || (key == "index" && "" == item.path)) {
  136. routes.push(item);
  137. }
  138. });
  139. }
  140. if(routes.length > 0) {
  141. this.$store.commit("SET_SIDEBAR_ROUTERS", routes);
  142. }
  143. return routes;
  144. }
  145. },
  146. };
  147. </script>
  148. <style lang="scss" scoped>
  149. .el-menu--horizontal > .el-menu-item {
  150. float: left;
  151. height: 50px;
  152. line-height: 50px;
  153. margin: 0;
  154. border-bottom: 3px solid transparent;
  155. color: #999093;
  156. padding: 0 5px;
  157. margin: 0 10px;
  158. }
  159. .el-menu--horizontal > .el-menu-item.is-active {
  160. border-bottom: 3px solid #409eff;
  161. color: #303133;
  162. }
  163. </style>