index.vue 5.8 KB

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