Angular2路由:导入带有路由的子模块,使其成为前缀
发布时间:2020-12-17 07:52:45 所属栏目:安全 来源:网络整理
导读:我有一个主模块和一些子模块.我想在它们之间指定一些不平凡的路由. 我更喜欢在子模块中定义子模块的路径.例如.: @NgModule({ imports: [ /*...*/ RouterModule.forChild([ { path: 'country',redirectTo: 'country/list' },{ path: 'country/list',componen
我有一个主模块和一些子模块.我想在它们之间指定一些不平凡的路由.
我更喜欢在子模块中定义子模块的路径.例如.: @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 我想通过其他路由访问其他模块,例如/ Otherstuff / city / create和/ otherstuff / city / list`下的CityModule. 我的问题: >是否可以导入具有自己的路由的模块并使其路由前缀? UPDATE 接受的答案是最好的方法:在模块中创建路径,在外部注册它们.因此,您可以修改路线,例如前缀(这是我想要的),你可以定义警卫,覆盖或过滤它们等.
在你的appRoutes中添加子路径
const appRoutes = [ { path: '',{ path: 'settings',component: CountryComponent,canActivate: [AuthGuard],children: COUNTRY_ROUTES },]; 创建单独的路由文件 export const COUNTRY_ROUTES:Routes = [ { path: 'country',]; 在CountryComponent.html中 <router-outlet></router-outlet> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |