【发布时间】:2019-10-17 10:47:58
【问题描述】:
我有一个名为“host”的模块,它有自己的路由,我想将它插入到 app-routing.module 中。但是,我遇到了先加载通配符并显示 PageNotFoundComponent,而不是加载 Host 组件的问题。我有以下文件。
host.module.ts
....
const routes: Routes = [
{
path: 'host',
children: [
{ path: '', component: HostComponent }
]
}
];
@NgModule({
declarations: [HostComponent],
imports: [
CommonModule,
RouterModule.forChild(routes)
]
})
export class HostModule { }
app-routing.module.ts
const routes: Routes = [
{ path: '', component: HomeComponent, pathMatch: "full"},
{ path: '**', component: PageNotFoundComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
app.module.ts
@NgModule({
declarations: [
AppComponent,
HomeComponent,
PageNotFoundComponent
],
imports: [
BrowserModule,
AppRoutingModule,
HostModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
app.component.html
<h2>Home</h2>
<ul>
<li>
<h2><a routerLink="/host">host</a></h2>
</li>
</ul>
<router-outlet></router-outlet>
问题:当我运行应用程序并单击“主机”按钮时,它会加载 PageNotFoundComponent。我显然希望它转到 HostComponent。
【问题讨论】:
-
预期结果?我没有看到您有“主机”的路由路径,或者它是延迟加载的。
-
@penleychan 请在原始问题中查看我的更新。在 app.component.html 中有 Host 的 routerLink
-
再次,
HostModule的使用/定义位置和方式。如果您只是将模块导入AppModule,则需要管理您的导入顺序。配置中路由的顺序很重要,这是设计使然。 -
@penleychan 哦,我想我明白你在问什么。我更新了 app.module.ts 文件。请看
标签: javascript angular single-page-application angular7 angular7-router