【发布时间】:2019-01-03 16:45:37
【问题描述】:
我正在开发一个 Angular 6 应用程序。目前我正在为路由而苦苦挣扎。我很感兴趣,无论我的结构,我想象的是否可以工作。所以它看起来像这样:
App 模块 - 包含主路由和一些父路由,其中定义了布局。像这样:
const routes: Routes = [
{
path: 'login',
component: LoginComponent
},
{
path: '',
component: LayoutComponent,
canActivate: [AuthGuard],
canActivateChild: [AuthGuard],
children: [
// {
// path: 'brands',
// loadChildren: 'app/modules/brands/brands.module#BrandsModule',
// pathMatch: 'prefix'
// }
]
}
];
@NgModule({
imports: [RouterModule.forRoot(routes), BrandsModule, ItemsModule],
exports: [RouterModule],
providers: [BosRouteLoader]
})
export class RoutingModule {}
我的一个功能模块在这样的模块中定义了自己的路由:
const routes: Routes = [
{
path: 'brands',
children: [
{ path: '', component: BrandListComponent },
{ path: ':id', component: BrandDetailComponent },
{ path: '**', redirectTo: '' }
]
}];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class BrandsRoutingModule {}
我想实现每个功能模块都定义自己的路由,并将这些路由注册为 App 模块的子路由。
通过延迟加载,我可以管理它,但我必须在我的 App 模块中再定义一个路由,但我只想在功能模块中定义它。
如果我在没有延迟加载的情况下执行此操作,那么我在 App 组件中的父路由永远不会被命中。所以如果我去 http://localhost/brands 它将加载适当的 BrandLisComponent 但没有 LayoutComponent。
有没有办法在功能模块中定义路由并将它们注册为主路由模块的子模块?
【问题讨论】:
-
如果你为
BrandRountingModule使用子路由,你需要给它一个component属性,而不仅仅是children!然后使用<router-outlet>标签来解决这些孩子。但通常我没有得到你想要达到的目标。什么是 ItemListComponent 和 localhost/items ?! -
@imans77 我想实现,如果我去 /brands 路线然后 LayoutComponent 和 BrandListComponent 将是活动的。现在只有brandComponent处于活动状态,没有布局......
标签: javascript angular typescript routing