在Angular2 NGmodule中。我想根据Condition加载2组不同的路由模块

时间:2016-11-13 11:56:16

标签: javascript angular typescript rxjs

在Angular2中。我想为求职者和其他雇主加载2套不同的路线模块1。网址是相同的,但求职者和雇主的模块将是不同的。因此,我想在登录会话中动态加载路由模块。是否有办法可以做到这一点?或者是否有更好的方法可以在angular2

中实现

基本上我希望能够根据条件

加载ngModule

以下是示例代码。

@NgModule({
  imports: [BrowserModule, HttpModule,customRoutes.getCustomRoutes(),
            SharedModule.forRoot()],
  declarations: [AppComponent],
  providers: [
    {
    provide: APP_BASE_HREF,
    useValue: '<%= APP_BASE %>'  },
    CanActivateGuard,AccountService, ProfileService],
  bootstrap: [AppComponent]
})

export class AppModule {

  constructor(private _http:Http,@Inject(AccountService) authService:AccountService) {


  }

//自定义路线

export class customRoutes {

  public customRoutes;
  constructor(@Inject(AccountService)  accountService:AccountService) {


  }

 getCustomRoutes() {
    return this.customRoutes = (accountService.getCheckEmployer())?JobSeekerRoutes:EmployerRoutes;
  }

}

//但是getCustomRoutes需要是一个在NGModule中调用的静态方法。但如果我将其设为静态,我将可以访问accountService对象。

(有没有更好的方法来实现基于用户会话加载不同路由的功能)

1 个答案:

答案 0 :(得分:0)

不幸的是,目前不支持在运行时配置路由。当Angular应用程序被引导时指定了路由,之后无法更改它们。

相关问题