index.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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
  10. v-if="item.meta && item.meta.icon && item.meta.icon !== '#'"
  11. :icon-class="item.meta.icon"
  12. />
  13. {{ item.meta.title }}</el-menu-item
  14. >
  15. </template>
  16. <!-- 顶部菜单超出数量折叠 -->
  17. <el-submenu :style="{'--theme': theme}" index="more" v-if="topMenus.length > visibleNumber">
  18. <template slot="title">更多菜单</template>
  19. <template v-for="(item, index) in topMenus">
  20. <el-menu-item
  21. :index="item.path"
  22. :key="index"
  23. v-if="index >= visibleNumber"
  24. ><svg-icon :icon-class="item.meta.icon" />
  25. {{ item.meta.title }}</el-menu-item
  26. >
  27. </template>
  28. </el-submenu>
  29. </el-menu>
  30. </template>
  31. <script>
  32. import { constantRoutes } from "@/router";
  33. // 隐藏侧边栏路由
  34. const hideList = ['/index', '/user/profile'];
  35. export default {
  36. data() {
  37. return {
  38. // 顶部栏初始数
  39. visibleNumber: 5,
  40. // 当前激活菜单的 index
  41. currentIndex: undefined
  42. };
  43. },
  44. computed: {
  45. theme() {
  46. return this.$store.state.settings.theme;
  47. },
  48. // 顶部显示菜单
  49. topMenus() {
  50. let topMenus = [];
  51. this.routers.map((menu) => {
  52. if (menu.hidden !== true) {
  53. // 兼容顶部栏一级菜单内部跳转
  54. if (menu.path === "/") {
  55. topMenus.push(menu.children[0]);
  56. } else {
  57. topMenus.push(menu);
  58. }
  59. }
  60. });
  61. return topMenus;
  62. },
  63. // 所有的路由信息
  64. routers() {
  65. return this.$store.state.permission.topbarRouters;
  66. },
  67. // 设置子路由
  68. childrenMenus() {
  69. var childrenMenus = [];
  70. this.routers.map((router) => {
  71. for (var item in router.children) {
  72. if (router.children[item].parentPath === undefined) {
  73. if(router.path === "/") {
  74. router.children[item].path = "/" + router.children[item].path;
  75. } else {
  76. if(!this.ishttp(router.children[item].path)) {
  77. router.children[item].path = router.path + "/" + router.children[item].path;
  78. }
  79. }
  80. router.children[item].parentPath = router.path;
  81. }
  82. childrenMenus.push(router.children[item]);
  83. }
  84. });
  85. return constantRoutes.concat(childrenMenus);
  86. },
  87. // 默认激活的菜单
  88. activeMenu() {
  89. const path = this.$route.path;
  90. let activePath = path;
  91. if (path !== undefined && path.lastIndexOf("/") > 0 && hideList.indexOf(path) === -1) {
  92. const tmpPath = path.substring(1, path.length);
  93. activePath = "/" + tmpPath.substring(0, tmpPath.indexOf("/"));
  94. if (!this.$route.meta.link) {
  95. this.$store.dispatch('app/toggleSideBarHide', false);
  96. }
  97. } else if(!this.$route.children) {
  98. activePath = path;
  99. this.$store.dispatch('app/toggleSideBarHide', true);
  100. }
  101. this.activeRoutes(activePath);
  102. return activePath;
  103. },
  104. },
  105. beforeMount() {
  106. window.addEventListener('resize', this.setVisibleNumber)
  107. },
  108. beforeDestroy() {
  109. window.removeEventListener('resize', this.setVisibleNumber)
  110. },
  111. mounted() {
  112. this.setVisibleNumber();
  113. },
  114. methods: {
  115. // 根据宽度计算设置显示栏数
  116. setVisibleNumber() {
  117. const width = document.body.getBoundingClientRect().width / 3;
  118. this.visibleNumber = parseInt(width / 85);
  119. },
  120. // 菜单选择事件
  121. handleSelect(key, keyPath) {
  122. this.currentIndex = key;
  123. const route = this.routers.find(item => item.path === key);
  124. if (this.ishttp(key)) {
  125. // http(s):// 路径新窗口打开
  126. window.open(key, "_blank");
  127. } else if (!route || !route.children) {
  128. // 没有子路由路径内部打开
  129. const routeMenu = this.childrenMenus.find(item => item.path === key);
  130. if (routeMenu && routeMenu.query) {
  131. let query = JSON.parse(routeMenu.query);
  132. this.$router.push({ path: key, query: query });
  133. } else {
  134. this.$router.push({ path: key });
  135. }
  136. this.$store.dispatch('app/toggleSideBarHide', true);
  137. } else {
  138. // 显示左侧联动菜单
  139. this.activeRoutes(key);
  140. this.$store.dispatch('app/toggleSideBarHide', false);
  141. }
  142. },
  143. // 当前激活的路由
  144. activeRoutes(key) {
  145. var routes = [];
  146. if (this.childrenMenus && this.childrenMenus.length > 0) {
  147. this.childrenMenus.map((item) => {
  148. if (key == item.parentPath || (key == "index" && "" == item.path)) {
  149. routes.push(item);
  150. }
  151. });
  152. }
  153. if(routes.length > 0) {
  154. this.$store.commit("SET_SIDEBAR_ROUTERS", routes);
  155. } else {
  156. this.$store.dispatch('app/toggleSideBarHide', true);
  157. }
  158. },
  159. ishttp(url) {
  160. return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1
  161. }
  162. },
  163. };
  164. </script>
  165. <style lang="scss">
  166. .topmenu-container.el-menu--horizontal > .el-menu-item {
  167. float: left;
  168. height: 50px !important;
  169. line-height: 50px !important;
  170. color: #999093 !important;
  171. padding: 0 5px !important;
  172. margin: 0 10px !important;
  173. }
  174. .topmenu-container.el-menu--horizontal > .el-menu-item.is-active, .el-menu--horizontal > .el-submenu.is-active .el-submenu__title {
  175. border-bottom: 2px solid #{'var(--theme)'} !important;
  176. color: #303133;
  177. }
  178. /* submenu item */
  179. .topmenu-container.el-menu--horizontal > .el-submenu .el-submenu__title {
  180. float: left;
  181. height: 50px !important;
  182. line-height: 50px !important;
  183. color: #999093 !important;
  184. padding: 0 5px !important;
  185. margin: 0 10px !important;
  186. }
  187. </style>