Angular2 Routing:导入带有路由的子模块,并使其带有前缀

加博尔·伊姆雷

我有一个主模块和一些子模块。我想在它们之间指定一些不简单的路由。

我更喜欢在子模块中定义子模块的路由。例如:

@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

我还想通过其他路由访问其他模块,例如CityModuleunder/otherstuff/city/create和/ otherstuff / city / list`。

我的问题:

  1. 是否可以使用自己的路由导入模块并为其路由添加前缀?
  2. 此外:是否有一种方法可以在两个子模块之间建立与其最终(前缀)路由无关的链接?

更新

可接受的答案是最好的方法:在模块中创建路由,在外部注册它们。因此,您可以修改路由,例如为它们添加前缀(这是我想要的),可以定义防护,覆盖或过滤它们,等等。

阿育·阿里·萨克

在您的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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章