index.vue 5.5 KB

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