SidebarItem.vue
2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<template>
<div v-if="!item.hidden&&item.children" class="menu-wrapper">
<router-link
v-if="hasOneShowingChild(item.children) && !onlyOneChild.children&&!item.alwaysShow"
:to="resolvePath(onlyOneChild.path)"
>
<el-menu-item
:index="resolvePath(onlyOneChild.path)"
:class="{'submenu-title-noDropdown':!isNest}"
>
<svg-icon
v-if="onlyOneChild.meta&&onlyOneChild.meta.icon"
:icon-class="onlyOneChild.meta.icon"
></svg-icon>
<span
v-if="onlyOneChild.meta&&onlyOneChild.meta.title"
slot="title"
>{{onlyOneChild.meta.title}}</span>
</el-menu-item>
</router-link>
<el-submenu v-else :index="item.name||item.path">
<template slot="title">
<svg-icon v-if="item.meta&&item.meta.icon" :icon-class="item.meta.icon"></svg-icon>
<span v-if="item.meta&&item.meta.title" slot="title">{{item.meta.title}}</span>
</template>
<template v-for="child in item.children" v-if="!child.hidden">
<sidebar-item
:is-nest="true"
class="nest-menu"
v-if="child.children&&child.children.length>0"
:item="child"
:key="child.path"
:base-path="resolvePath(child.path)"
></sidebar-item>
<router-link v-else :to="resolvePath(child.path)" :key="child.name">
<el-menu-item :index="resolvePath(child.path)">
<svg-icon v-if="child.meta&&child.meta.icon" :icon-class="child.meta.icon"></svg-icon>
<span v-if="child.meta&&child.meta.title" slot="title">{{child.meta.title}}</span>
</el-menu-item>
</router-link>
</template>
</el-submenu>
</div>
</template>
<script>
import path from "path";
export default {
name: "SidebarItem",
props: {
// route配置json
item: {
type: Object,
required: true
},
isNest: {
type: Boolean,
default: false
},
basePath: {
type: String,
default: ""
}
},
data() {
return {
onlyOneChild: null
};
},
methods: {
hasOneShowingChild(children) {
const showingChildren = children.filter(item => {
if (item.hidden) {
return false;
} else {
this.onlyOneChild = item;
return true;
}
});
if (showingChildren.length === 1) {
return true;
}
return false;
},
resolvePath(...paths) {
return path.resolve(this.basePath, ...paths);
}
}
};
</script>
<style lang="scss" scoped>
@import "@/styles/color.scss";
.menu-wrapper {
/deep/ .el-menu-item,
.el-submenu__title {
height: 46px;
line-height: 46px;
}
/deep/ a {
background-color: #ffc248;
}
/deep/ .el-menu-item.is-active {
margin-left: 8px;
background-color: $Theme-color !important;
}
/deep/ .el-menu-item {
padding: 0 20px 0 12px;
}
}
</style>