【问题标题】:In Angular2 NGmodule .I want to load 2 different sets of route modules based on Condition在 Angular2 NGmodule 中。我想根据条件加载 2 组不同的路由模块
【发布时间】:2017-03-27 04:37:37
【问题描述】:

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

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

下面是示例代码。

@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 对象。

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

【问题讨论】:

    标签: javascript angular typescript rxjs


    【解决方案1】:

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

    【讨论】:

    • 如果我想根据特定条件加载模块,这可能吗?假设如果设置了雇主会话,我需要加载特定于雇主的模块
    猜你喜欢
    • 2018-01-19
    • 2018-09-20
    • 1970-01-01
    • 1970-01-01
    • 2017-11-07
    • 2017-08-21
    • 2020-01-12
    • 2021-05-26
    • 1970-01-01
    相关资源
    最近更新 更多