【发布时间】:2019-10-23 10:28:06
【问题描述】:
正如你在 stackblitz 中看到的那样,我对 forRoot/forChild 路由有点坚持:
https://stackblitz.com/edit/hello-angular-6-thzmnv
在根模块中定义了路由“子路由”:
const routes: Routes = [
{
path: 'sub-route',
loadChildren: () => SubModule,
},
];
@NgModule({
imports: [
BrowserModule,
SubModule,
RouterModule.forRoot(routes),
SubRoutingModule
],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
在子路由模块中定义了同伴路由:
const routes: Routes = [
{
path: '',
component: SubComponent,
},
{
path: 'first',
component: SubComponent,
data: { index: 1 },
},
{
path: 'second',
component: SubComponent,
data: { index: 2 },
},
{
path: '**',
redirectTo: 'first',
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class SubRoutingModule {}
我不明白为什么后备路由 ** 没有按预期工作。
例如,导航到 /sub-route/second 将重定向到 /first。如果定义了合适的路线,为什么会发生重定向?如果由于某种原因发生了重定向,为什么它不重定向到/sub-route/first,而只是重定向到/first?谢谢!
【问题讨论】: