index.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. // 不需要激活的路由
  31. const noactiveList = ["/user/profile", "/dict/type", "/gen/edit", "/job/log"];
  32. export default {
  33. data() {
  34. return {
  35. // 顶部栏初始数
  36. visibleNumber: 5,
  37. // 是否为首次加载
  38. isFrist: false,
  39. };
  40. },
  41. computed: {
  42. // 顶部显示菜单
  43. topMenus() {
  44. let topMenus = [];
  45. this.routers.map((menu) => {
  46. if (menu.hidden !== true) {
  47. topMenus.push(menu);
  48. }
  49. });
  50. return topMenus;
  51. },
  52. // 所有的路由信息
  53. routers() {
  54. return this.$store.state.permission.topbarRouters;
  55. },
  56. // 设置子路由
  57. childrenMenus() {
  58. var childrenMenus = [];
  59. this.routers.map((router) => {
  60. for (var item in router.children) {
  61. if (router.children[item].parentPath === undefined) {
  62. router.children[item].path = router.path + "/" + router.children[item].path;
  63. router.children[item].parentPath = router.path;
  64. }
  65. childrenMenus.push(router.children[item]);
  66. }
  67. });
  68. return constantRoutes.concat(childrenMenus);
  69. },
  70. // 默认激活的菜单
  71. activeMenu() {
  72. const path = this.$route.path;
  73. let activePath = this.routers[0].path;
  74. var noactive = noactiveList.some(function (item) {
  75. return path.indexOf(item) !== -1;
  76. });
  77. if (noactive) {
  78. return;
  79. }
  80. if (path.lastIndexOf("/") > 0) {
  81. const tmpPath = path.substring(1, path.length);
  82. activePath = "/" + tmpPath.substring(0, tmpPath.indexOf("/"));
  83. } else if ("/index" == path || "" == path) {
  84. if (!this.isFrist) {
  85. this.isFrist = true;
  86. } else {
  87. activePath = "index";
  88. }
  89. }
  90. this.activeRoutes(activePath);
  91. return activePath;
  92. },
  93. },
  94. beforeMount() {
  95. window.addEventListener('resize', this.setVisibleNumber)
  96. },
  97. beforeDestroy() {
  98. window.removeEventListener('resize', this.setVisibleNumber)
  99. },
  100. mounted() {
  101. this.setVisibleNumber();
  102. },
  103. methods: {
  104. // 根据宽度计算设置显示栏数
  105. setVisibleNumber() {
  106. const width = document.body.getBoundingClientRect().width / 3;
  107. this.visibleNumber = parseInt(width / 85);
  108. },
  109. // 菜单选择事件
  110. handleSelect(key, keyPath) {
  111. if (key.indexOf("http://") !== -1 || key.indexOf("https://") !== -1) {
  112. // http(s):// 路径新窗口打开
  113. window.open(key, "_blank");
  114. } else {
  115. this.activeRoutes(key);
  116. }
  117. },
  118. // 当前激活的路由
  119. activeRoutes(key) {
  120. var routes = [];
  121. if (this.childrenMenus && this.childrenMenus.length > 0) {
  122. this.childrenMenus.map((item) => {
  123. if (key == item.parentPath || (key == "index" && "" == item.path)) {
  124. routes.push(item);
  125. }
  126. });
  127. }
  128. this.$store.commit("SET_SIDEBAR_ROUTERS", routes);
  129. }
  130. },
  131. };
  132. </script>
  133. <style lang="scss" scoped>
  134. .el-menu--horizontal > .el-menu-item {
  135. float: left;
  136. height: 50px;
  137. line-height: 50px;
  138. margin: 0;
  139. border-bottom: 3px solid transparent;
  140. color: #999093;
  141. padding: 0 5px;
  142. margin: 0 10px;
  143. }
  144. .el-menu--horizontal > .el-menu-item.is-active {
  145. border-bottom: 3px solid #409eff;
  146. color: #303133;
  147. }
  148. </style>