在使用Angular 4时,如果默认子路由不起作用,可以尝试以下解决方法:
redirectTo
属性为默认子路由的路径。const routes: Routes = [
{
path: 'parent',
component: ParentComponent,
children: [
{
path: '',
redirectTo: 'default-child',
pathMatch: 'full'
},
{
path: 'default-child',
component: DefaultChildComponent
},
{
path: 'other-child',
component: OtherChildComponent
}
]
}
];
指令,用于渲染子路由组件。
Default Child
Other Child
declarations
和exports
数组中。@NgModule({
declarations: [ParentComponent, DefaultChildComponent, OtherChildComponent],
exports: [ParentComponent, DefaultChildComponent, OtherChildComponent],
imports: [
CommonModule,
RouterModule.forChild(routes)
]
})
export class ParentModule { }
通过检查以上几个方面,应该能够解决默认子路由不起作用的问题。如果问题仍然存在,可以尝试重启应用或清除浏览器缓存。
下一篇:Angular 4全选复选框