问题描述:
在Angular 6中,当使用forChild
方法定义子模块的路由时,如果设置了fallback路由,会导致子路由无法正常工作。
解决方法:
const childRoutes: Routes = [
{ path: 'child', component: ChildComponent },
{ path: '**', redirectTo: 'child' } // fallback路由
];
@NgModule({
imports: [RouterModule.forChild(childRoutes)],
exports: [RouterModule]
})
export class ChildRoutingModule {}
forChild
方法引入子模块的路由。const routes: Routes = [
{ path: 'parent', component: ParentComponent },
{ path: 'parent/child', loadChildren: './child/child.module#ChildModule' }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}
通过以上配置,子路由将会正常工作,并且fallback路由只会在没有匹配的子路由时才会触发。