-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
41d5624
commit 7abc316
Showing
3 changed files
with
41 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,46 @@ | ||
<template> | ||
<template v-if="!item.meta?.hideMenu"> | ||
<template | ||
v-if=" | ||
onlyOneChild && (!onlyOneChild.children?.length || onlyOneChild.noShowingChildren) && !item.meta?.alwaysShow | ||
" | ||
> | ||
<template v-if="menu"> | ||
<el-sub-menu v-if="menu.children?.length" :index="menu.path"> | ||
<template v-if="menu.meta" #title> | ||
<component :is="menu.meta!.icon" v-if="menu.meta!.icon" /> | ||
<span class="menu">{{ $t(menu.meta!.title!) }}</span> | ||
</template> | ||
<layout-menu-item v-for="child in menu.children" :key="child.path" :item="child"></layout-menu-item> | ||
</el-sub-menu> | ||
<template v-else> | ||
<component | ||
:is="onlyOneChild.meta?.isLink ? 'a' : 'routerLink'" | ||
v-if="onlyOneChild.meta && onlyOneChild.meta.title" | ||
:href="onlyOneChild.path" | ||
:to="onlyOneChild.path" | ||
:is="menu.meta?.isLink ? 'a' : 'routerLink'" | ||
v-if="menu.meta && menu.meta.title" | ||
:href="menu.path" | ||
:to="menu.path" | ||
> | ||
<el-menu-item :index="onlyOneChild.path" :title="$t(onlyOneChild.meta.title)"> | ||
<component :is="onlyOneChild.meta.icon" v-if="onlyOneChild.meta.icon" /> | ||
<el-menu-item :index="menu.path" :title="$t(menu.meta.title)"> | ||
<component :is="menu.meta.icon" v-if="menu.meta.icon" /> | ||
<template #title> | ||
<span class="menu">{{ $t(onlyOneChild.meta.title) }}</span> | ||
<span class="menu">{{ $t(menu.meta.title) }}</span> | ||
</template> | ||
</el-menu-item> | ||
</component> | ||
</template> | ||
<el-sub-menu v-else :index="item.path"> | ||
<template v-if="item.meta" #title> | ||
<component :is="item.meta!.icon" v-if="item.meta!.icon" /> | ||
<span class="menu">{{ $t(item.meta!.title!) }}</span> | ||
</template> | ||
<layout-menu-item v-for="child in item.children" :key="child.path" :item="child"></layout-menu-item> | ||
</el-sub-menu> | ||
</template> | ||
</template> | ||
|
||
<script setup lang="ts" name="MenuItem"> | ||
import { RouteRecordRaw } from 'vue-router'; | ||
const props = defineProps<{ item: RouteRecordRaw }>(); | ||
let onlyOneChild = ref<RouteRecordRaw>(); | ||
const hasOneShowingChild = (children = [] as RouteRecordRaw[], parent: RouteRecordRaw) => { | ||
const showingChildren = children.filter((item) => item.meta && !item.meta.hideMenu); | ||
if (showingChildren.length === 1) { | ||
onlyOneChild.value = showingChildren[0]; | ||
return true; | ||
const menu = ref<RouteRecordRaw>(); | ||
const getMenu = (item: RouteRecordRaw): RouteRecordRaw => { | ||
if (!item.children?.length) { | ||
return item; | ||
} | ||
if (showingChildren.length === 0) { | ||
onlyOneChild.value = { ...parent, noShowingChildren: true }; | ||
return true; | ||
const children = item.children.filter((v) => v.meta && !v.meta.hideMenu); | ||
const res = { ...item, children: children }; | ||
if (!item.meta?.alwaysShow && children.length === 1) { | ||
return getMenu(children[0]); | ||
} | ||
return false; | ||
return res; | ||
}; | ||
hasOneShowingChild(props.item.children, props.item); | ||
if (!props.item.meta?.hideMenu) { | ||
menu.value = getMenu(props.item); | ||
} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters