Skip to content

Commit adec74c

Browse files
committed
fixed: sidebar has no children bug
1 parent 8df7322 commit adec74c

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/views/layout/components/Sidebar/SidebarItem.vue

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div v-if="!item.hidden&&item.children" class="menu-wrapper">
33

4-
<template v-if="hasOneShowingChild(item.children) && !onlyOneChild.children&&!item.alwaysShow">
4+
<template v-if="hasOneShowingChild(item.children,item) && (!onlyOneChild.children||onlyOneChild.noShowingChildren)&&!item.alwaysShow">
55
<a :href="onlyOneChild.path" target="_blank" @click="clickLink(onlyOneChild.path,$event)">
66
<el-menu-item :index="resolvePath(onlyOneChild.path)" :class="{'submenu-title-noDropdown':!isNest}">
77
<item v-if="onlyOneChild.meta" :icon="onlyOneChild.meta.icon||item.meta.icon" :title="onlyOneChild.meta.title" />
@@ -21,8 +21,7 @@
2121
:item="child"
2222
:key="child.path"
2323
:base-path="resolvePath(child.path)"
24-
class="nest-menu"/>
25-
24+
class="nest-menu" />
2625
<a v-else :href="child.path" :key="child.name" target="_blank" @click="clickLink(child.path,$event)">
2726
<el-menu-item :index="resolvePath(child.path)">
2827
<item v-if="child.meta" :icon="child.meta.icon" :title="child.meta.title" />
@@ -43,7 +42,7 @@ export default {
4342
name: 'SidebarItem',
4443
components: { Item },
4544
props: {
46-
// route配置json
45+
// route object
4746
item: {
4847
type: Object,
4948
required: true
@@ -63,19 +62,28 @@ export default {
6362
}
6463
},
6564
methods: {
66-
hasOneShowingChild(children) {
65+
hasOneShowingChild(children, parent) {
6766
const showingChildren = children.filter(item => {
6867
if (item.hidden) {
6968
return false
7069
} else {
71-
// temp set(will be used if only has one showing child )
70+
// Temp set(will be used if only has one showing child)
7271
this.onlyOneChild = item
7372
return true
7473
}
7574
})
75+
76+
// When there is only one child router, the child router is displayed by default
7677
if (showingChildren.length === 1) {
7778
return true
7879
}
80+
81+
// Show parent if there are no child router to display
82+
if (showingChildren.length === 0) {
83+
this.onlyOneChild = { ... parent, path: '', noShowingChildren: true }
84+
return true
85+
}
86+
7987
return false
8088
},
8189
resolvePath(routePath) {

0 commit comments

Comments
 (0)