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