angular - 默认路由不起作用 [Angular4]
问题描述
我厌倦了任何事情,但它只发生在这个项目中。我使用了相同的路由代码,但默认路由不起作用。我需要修复代码还是需要修复其他地方?
此代码在app.moules.ts中:
export const AppRoutes: Routes = [
{
path: '',
redirectTo: '/login',
pathMatch: 'full'
}, {
path: '',
component: AdminLayoutComponent,
children: [
{
path: 'main',
component: MainComponent,
data: {
breadcrumb: 'Main',
status: true
}
}, {
path: 'userterminal',
loadChildren: './userterminal/userterminal.module.ts#UserTerminalModule'
}, {
path: 'userslogin',
loadChildren: './user-login/userlogin.module.ts#UserloginModule'
}, {
path: 'cameraconfig',
loadChildren: './camera-config/camera-
config.module.ts#CameraConfigModule'
}
]
}, {
path: '',
component: AuthLayoutComponent,
children: [
{
path: 'authentication',
loadChildren: './authentication/authentication.module#AuthenticationModule'
}, {
path: 'error',
loadChildren: './error/error.module#ErrorModule'
}, {
path: 'login',
component: LoginComponent,
}, {
path: 'cameraLogin',
component: CamLoginComponent
}
]
}, {
path: '**',
redirectTo: 'error/404'
}
];
解决方案
在您的 Routes 中找到的第一个空路由重定向到/login
.
当您使用children
它时,意味着您的父组件拥有它自己的router-outlet
.
我假设您 LoginComponent 在您的路由的基础上,所以它path
也应该在根目录下。像这样的东西:
export const AppRoutes: Routes = [
{
path: '',
redirectTo: '/login',
pathMatch: 'full'
},
{
path: 'main',
component: AdminLayoutComponent,
children: [
{
path: '',
component: MainComponent,
data: {
breadcrumb: 'Main',
status: true
}
},
{
path: 'userterminal',
loadChildren: './userterminal/userterminal.module.ts#UserTerminalModule'
}
]
},
{
path: 'login',
component: LoginComponent
}
];
当您使用重定向时,使用这样的结构,它会找到路径,因为它位于根目录。
使用,您可以main
从登录成功导航到。请记住,如果您使用的是子组件,则父组件示例AdminLayoutComponent
必须具有 arouter-outlet
才能在其中加载它的子组件。
推荐阅读
- python - 仅使用 python 在子目录中查找文件
- excel - VBA:根据第二列中的出现次数查找一列中的出现次数
- karate - 如何使用从 karate-config.js 到 .feature 文件的变量
- apache-camel - 如何在 apache camel 中发送空响应以进行休息呼叫
- ssl - 连接到生产者时出错:错误:ssl.certificate.location 失败
- opencv - opencv:显示图像并等待变量更改,而不是等待按键
- db2 - 如何更改 SYSIBMADM.PRIVILEGES 表中的 GRANTABLE?
- python - 在烧瓶中使用 SQL alchemy 将数据插入 Postgres
- json - 使用 FLINK 将 JSON 下沉到 Kafka 的最快方法
- python - 数据框转换和字符串连接