gpt4 book ai didi

Angular6 - 未加载空路径的子路由

转载 作者:太空狗 更新时间:2023-10-29 17:40:11 25 4
gpt4 key购买 nike

我正在将我的 Angular 4 应用程序转换为 Angular 6。但是,我在进行与路由相关的更改时遇到了问题。

现在,当我运行我的应用程序时,Home 组件已加载,但未加载具有空路径的子组件 ContentsComponent

我的应用程序中的路由定义如下:

app.routing.module.ts

const routes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'support', component: SupportComponent }
];

@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})

export class AppRoutingModule { }

home.routing.module.ts

const routes: Routes = [
{
path: 'home', component: HomeComponent,
children: [
{ path: '', component: ContentsComponent }, //<-- This component is not loaded
{ path: '**', redirectTo: 'home' }
]
}
];

@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})

export class HomeRoutingModule { }

home.module.ts

@NgModule({
imports: [
HomeRoutingModule
],
exports: [],
declarations: [ HomeComponent, ContentsComponent ],
providers: [],
})

export class HomeModule { }

app.module.ts

@NgModule({
imports: [
BrowserModule,
AppRoutingModule,
ReactiveFormsModule,
HomeModule
],
declarations: [AppComponent],
bootstrap: [AppComponent],
providers: [
{ provide: LocationStrategy, useClass: HashLocationStrategy }
]
})
export class AppModule { }

home.component.html

<router-outlet></router-outlet>

编辑:请注意,我不想在 /home url 部分之后显示任何内容。

最佳答案

这是因为相对路径。尝试以下应该有效。

children: [
{ path: '', redirectTo: '/home/content' },
{ path: 'content ', component: ContentsComponent },
{ path: '**', redirectTo: 'home' }
]

关于Angular6 - 未加载空路径的子路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51930936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com