index.vue 3.9 KB

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