Files
Snap.Server.Web/src/components/SidebarItem.vue

57 lines
1.5 KiB
Vue
Raw Normal View History

2025-12-20 15:38:00 +08:00
<template>
<template v-for="item in routes" :key="item.path">
<!-- 有子路由 -->
<el-sub-menu
v-if="hasChildren(item)"
2026-01-29 13:15:18 +08:00
:index="getFullPath(item)"
2025-12-20 15:38:00 +08:00
>
<template #title>
<el-icon v-if="item.meta?.icon">
<component :is="icons[item.meta.icon as keyof typeof icons]" />
</el-icon>
<span>{{ item.meta?.title }}</span>
</template>
2026-01-29 13:15:18 +08:00
<SidebarItem :routes="item.children!" :parent-path="getFullPath(item)" />
2025-12-20 15:38:00 +08:00
</el-sub-menu>
<!-- 普通菜单 -->
<el-menu-item
v-else
:index="getFullPath(item)"
2025-12-20 15:38:00 +08:00
>
<el-icon v-if="item.meta?.icon">
<component :is="icons[item.meta.icon as keyof typeof icons]" />
</el-icon>
<span>{{ item.meta?.title }}</span>
</el-menu-item>
</template>
</template>
<script setup lang="ts">
import type { RouteRecordRaw } from 'vue-router'
import * as icons from '@element-plus/icons-vue'
interface Props {
2025-12-20 15:38:00 +08:00
routes: RouteRecordRaw[]
parentPath?: string
}
const props = withDefaults(defineProps<Props>(), {
2026-01-29 13:15:18 +08:00
parentPath: '/dashboard'
})
2025-12-20 15:38:00 +08:00
const hasChildren = (route: RouteRecordRaw) =>
route.children && route.children.length > 0
const getFullPath = (route: RouteRecordRaw) => {
// 如果是绝对路径,直接返回
if (route.path.startsWith('/')) {
return route.path
}
// 否则拼接父级路径
2026-01-29 13:15:18 +08:00
const basePath = props.parentPath || ''
return basePath ? `${basePath}/${route.path}` : `/${route.path}`
}
2025-12-20 15:38:00 +08:00
</script>