如何在应用程序的多个位置设置路由。可能吗?

How to set up routes in more than one place in an application. Is it possible?

我有一个 Angular 2 应用程序正在被另一个应用程序作为组件库使用。消费的应用程序正在使用路由,它们在指定模块中配置,一切正常。

问题是除了库应用中定义的路由之外,导入此库的消费者应用还需要更多路由。所以我需要在消费者应用程序中设置这些路由。有没有办法在多个地方定义路由?

勾选Router Class,可以动态重新定义路由

router.resetConfig([
  // ...
]);

是的,您可以在任何模块中设置路由。我这里有一个例子:APM-Final-Updated 文件夹中的https://github.com/DeborahK/Angular2-GettingStarted

我在 app.module.ts 和 product.module.ts 都有路线。然后我们使用 imports 语句引入路由。

  imports: [
    BrowserModule,
    HttpModule,
    RouterModule.forRoot([
      { path: 'welcome', component: WelcomeComponent },
      { path: '', redirectTo: 'welcome', pathMatch: 'full' },
      { path: '**', redirectTo: 'welcome', pathMatch: 'full' }
    ]),
    ProductModule
  ],

是的,我们可以在任何模块中设置路由。这就是我们在 Angular 个应用程序中设置延迟加载的方式。

我要查看路由设置和延迟加载你可以查看我的 repo。

https://github.com/rahulrsingh09/footballdetails

应用模块代码

@NgModule({
  bootstrap: [ AppComponent],
  declarations: [
    AppComponent,CompetitionComponent,DropdownDirective
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    RouterModule,
    routing,
    //TableModule, Omitted for webpack lazy loading
    //TeamModule, Omitted for webpack lazy loading
    MaterialModule.forRoot(),
    LoadingAnimateModule.forRoot(),// temporary solution to loading
    ProgressBarModule
  ],
  providers:[CompetitionService,LoadingAnimateService ]// temporary solution to loading
})
export class AppModule {

}