修复路由问题

This commit is contained in:
fanbook-wangdage
2026-01-29 13:15:18 +08:00
parent 163248f90f
commit 079358a910
5 changed files with 17 additions and 8 deletions

View File

@@ -0,0 +1,7 @@
<template>
<router-view></router-view>
</template>
<script setup lang="ts">
// 这是一个占位组件,用于嵌套路由
</script>

View File

@@ -3,7 +3,7 @@
<!-- 有子路由 -->
<el-sub-menu
v-if="hasChildren(item)"
:index="item.path"
:index="getFullPath(item)"
>
<template #title>
<el-icon v-if="item.meta?.icon">
@@ -12,7 +12,7 @@
<span>{{ item.meta?.title }}</span>
</template>
<SidebarItem :routes="item.children!" :parent-path="item.path" />
<SidebarItem :routes="item.children!" :parent-path="getFullPath(item)" />
</el-sub-menu>
<!-- 普通菜单 -->
@@ -38,7 +38,7 @@ interface Props {
}
const props = withDefaults(defineProps<Props>(), {
parentPath: ''
parentPath: '/dashboard'
})
const hasChildren = (route: RouteRecordRaw) =>
@@ -50,6 +50,7 @@ const getFullPath = (route: RouteRecordRaw) => {
return route.path
}
// 否则拼接父级路径
return props.parentPath ? `/${props.parentPath}/${route.path}` : `/${route.path}`
const basePath = props.parentPath || ''
return basePath ? `${basePath}/${route.path}` : `/${route.path}`
}
</script>

View File

@@ -95,9 +95,9 @@ const activeMenu = computed(() => route.path)
* 获取菜单路由,直接使用配置的路由结构
*/
const menuRoutes = computed(() => {
// 直接返回路由配置中的子路由
const mainRoute = router.options.routes.find(r => r.path === '/')
return mainRoute?.children || []
// 查找 /dashboard 路由的子路由
const dashboardRoute = router.options.routes.find(r => r.path === '/dashboard')
return dashboardRoute?.children || []
})
</script>

View File

@@ -29,6 +29,7 @@ const routes = [
},
{
path: 'system',
component: () => import('@/components/RouterViewPlaceholder.vue'),
meta: { title: '系统管理', icon: 'Setting' },
children: [
{

View File

@@ -101,7 +101,7 @@ const handleLogin = async () => {
await userStore.fetchUserInfo()
ElMessage.success('登录成功')
router.push({ path: '/' })
router.push({ path: '/dashboard' })
} catch (error: any) {
ElMessage.error(error.message || '登录失败,请重试')
} finally {