我有一个主模块和一些子模块。我想在它们之间指定一些不简单的路由。
我更喜欢在子模块中定义子模块的路由。例如:
@NgModule({
imports: [
/*...*/
RouterModule.forChild([
{ path: 'country', redirectTo: 'country/list' },
{ path: 'country/list', component: CountryListComponent },
{ path: 'country/create', component: CountryCreateComponent },
/*...*/
])
],
declarations: [/*...*/],
exports: [
RouterModule,
],
})
export class CountryModule {}
我想使用自己的内部路由导入该模块,但希望将其整个路由作为前缀。
const appRoutes = [
{ path: '', component: HomeComponent },
/*... (basic routes)*/
];
@NgModule({
imports: [
/*...*/
RouterModule.forRoot(appRoutes),
CountryModule, // <- how to make its routing prefixed??
],
declarations: [
/*...*/
AppComponent,
],
bootstrap: [ AppComponent ]
})
export class AppModule {}
此设置创建以下路由:/country
,/country/list
等,但我想像这样给它们加上前缀:
/settings/country
/settings/country/list
/settings/country/create
我还想通过其他路由访问其他模块,例如CityModule
under/otherstuff/city/create
和/ otherstuff / city / list`。
我的问题:
更新
可接受的答案是最好的方法:在模块中创建路由,在外部注册它们。因此,您可以修改路由,例如为它们添加前缀(这是我想要的),可以定义防护,覆盖或过滤它们,等等。
在您的appRoutes中添加子路线,例如
const appRoutes = [
{ path: '', component: HomeComponent },
{
path: 'settings',
component: CountryComponent,
canActivate: [AuthGuard],
children: COUNTRY_ROUTES
},
];
创建一个单独的路线文件
export const COUNTRY_ROUTES:Routes = [
{ path: 'country', redirectTo: 'country/list' },
{ path: 'country/list', component: CountryListComponent },
{ path: 'country/create', component: CountryCreateComponent },
];
在CountryComponent.html中
<router-outlet></router-outlet>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句