index.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 }}
  11. </el-menu-item>
  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. >
  22. <svg-icon :icon-class="item.meta.icon"/>
  23. {{ item.meta.title }}
  24. </el-menu-item>
  25. </template>
  26. </el-submenu>
  27. </el-menu>
  28. </template>
  29. <script>
  30. import { constantRoutes } from "@/router";
  31. // 隐藏侧边栏路由
  32. const hideList = ['/index', '/user/profile'];
  33. export default {
  34. data() {
  35. return {
  36. // 顶部栏初始数
  37. visibleNumber: 5,
  38. // 当前激活菜单的 index
  39. currentIndex: undefined
  40. };
  41. },
  42. computed: {
  43. theme() {
  44. return this.$store.state.settings.theme;
  45. },
  46. // 顶部显示菜单
  47. topMenus() {
  48. let topMenus = [];
  49. this.routers.map((menu) => {
  50. if (menu.hidden !== true) {
  51. // 兼容顶部栏一级菜单内部跳转
  52. if (menu.path === "/") {
  53. topMenus.push(menu.children[0]);
  54. } else {
  55. topMenus.push(menu);
  56. }
  57. }
  58. });
  59. return topMenus;
  60. },
  61. // 所有的路由信息
  62. routers() {
  63. return this.$store.state.permission.topbarRouters;
  64. },
  65. // 设置子路由
  66. childrenMenus() {
  67. const childrenMenus = [];
  68. this.routers.map((router) => {
  69. for (let item in router.children) {
  70. if (router.children[item].parentPath === undefined) {
  71. if(router.path === "/") {
  72. router.children[item].path = "/" + router.children[item].path;
  73. } else {
  74. if(!this.ishttp(router.children[item].path)) {
  75. router.children[item].path = router.path + "/" + router.children[item].path;
  76. }
  77. }
  78. router.children[item].parentPath = router.path;
  79. }
  80. childrenMenus.push(router.children[item]);
  81. }
  82. });
  83. return constantRoutes.concat(childrenMenus);
  84. },
  85. // 默认激活的菜单
  86. activeMenu() {
  87. const path = this.$route.path;
  88. let activePath = path;
  89. if (path !== undefined && path.lastIndexOf("/") > 0 && hideList.indexOf(path) === -1) {
  90. const tmpPath = path.substring(1, path.length);
  91. activePath = "/" + tmpPath.substring(0, tmpPath.indexOf("/"));
  92. if (!this.$route.meta.link) {
  93. this.$store.dispatch('app/toggleSideBarHide', false)
  94. }
  95. } else if(!this.$route.children) {
  96. activePath = path;
  97. this.$store.dispatch('app/toggleSideBarHide', true);
  98. }
  99. this.activeRoutes(activePath);
  100. return activePath;
  101. },
  102. },
  103. beforeMount() {
  104. window.addEventListener('resize', this.setVisibleNumber)
  105. },
  106. beforeDestroy() {
  107. window.removeEventListener('resize', this.setVisibleNumber)
  108. },
  109. mounted() {
  110. this.setVisibleNumber();
  111. },
  112. methods: {
  113. // 根据宽度计算设置显示栏数
  114. setVisibleNumber() {
  115. const width = document.body.getBoundingClientRect().width / 3;
  116. this.visibleNumber = parseInt(width / 85);
  117. },
  118. // 菜单选择事件
  119. handleSelect(key, keyPath) {
  120. this.currentIndex = key;
  121. const route = this.routers.find(item => item.path === key);
  122. if (this.ishttp(key)) {
  123. // http(s):// 路径新窗口打开
  124. window.open(key, "_blank");
  125. } else if (!route || !route.children) {
  126. // 没有子路由路径内部打开
  127. this.$router.push({ path: key });
  128. this.$store.dispatch('app/toggleSideBarHide', true);
  129. } else {
  130. // 显示左侧联动菜单
  131. this.activeRoutes(key);
  132. if (!this.$route.meta.link) {
  133. this.$store.dispatch('app/toggleSideBarHide', false);
  134. }
  135. }
  136. },
  137. // 当前激活的路由
  138. activeRoutes(key) {
  139. const routes = []
  140. if (this.childrenMenus && this.childrenMenus.length > 0) {
  141. this.childrenMenus.map((item) => {
  142. if (key === item.parentPath || (key === "index" && "" === item.path)) {
  143. routes.push(item);
  144. }
  145. });
  146. }
  147. if(routes.length > 0) {
  148. this.$store.commit("SET_SIDEBAR_ROUTERS", routes);
  149. } else {
  150. this.$store.dispatch('app/toggleSideBarHide', true);
  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>