angular - 嵌套路由 Angular
问题描述
我试图在我的 myfile.routing.module.ts 文件中有两个级别的嵌套路由,但是当我尝试访问该路由时,它会重定向到主目录。这是我的代码:
路由.module.ts
.
.
.
const routes: Routes = [
{
path: '',
children: [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{
path: 'home',
component: HomeComponent,
children: [
{ path: '', redirectTo: 'page' },
{
path: 'page',
component: PageComponent,
canActivate: [PageGuard],
},
{
path: 'more', component: MoreComponent,
children: [
{ path: '', component: MoreComponent },
{ path: 'plants', component: PlantsComponent },
{ path: 'cars', component: CarsComponent },
{ path: 'pets', component: PetsComponent },
]},
],
},
],
},
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class myFileRoutingModule {}
在more.component.html我添加了<router-outlet></router-outlet>
在home.component.html我有这样的链接:
<a [routerLink]="['home/page/more/plants']">plants</a><br />
<a [routerLink]="['home/page/more/cars']">cars</a><br />
<a [routerLink]="['home/page/more/pets']">pets</a><br />
所以我遇到的问题是我无法访问每个部分(例如主页/页面/更多/植物),因为它一直重定向到主页。
有谁知道是什么问题?
解决方案
你不需要第一个空级别,
这可能会解决您的问题:
const routes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{
path: 'home',
component: HomeComponent,
children: [
{ path: '', redirectTo: 'page' },
{
path: 'page',
component: PageComponent,
canActivate: [PageGuard],
},
{
path: 'more', component: MoreComponent,
children: [
{ path: '', component: MoreComponent },
{ path: 'plants', component: PlantsComponent },
{ path: 'cars', component: CarsComponent },
{ path: 'pets', component: PetsComponent },
]},
],
}
];
请注意,所有子组件都必须包含<router-outlet></router-outlet>
HomeComponent 和 MoreComponent
推荐阅读
- c# - 如何获取 xaml 的内容
在 .cs 中? - asp.net-core - 如何在 IIS 上部署 beta 版本?
- python - 如何遍历 Python 列表及其列表元素,在每个元素中拆分字符串并写出新列表
- python - 带有 2 个定义相同方法的 Mixin 的 CBV - 如何让每个 Mixin 执行自己的代码?
- html - 将“https:”附加到以 src=// 开头的汤中的链接
- php - Cakephp 3 - 使用文件上传将大型 CSV 导入数据库 错误 503 服务不可用
- reactjs - React Native App 总是在登录屏幕上打开
- flutter - 在 Flutter 中的 FocusedBorder 上 TextFormFeild 的 LabelText 无法正确显示
- c# - 如何使用 MemoryStream 将数据从一种形式发送到另一种形式
- java - 如何从android textview(多行)中删除一个单词而不影响其他单词的对齐/位置