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

标签 javascript angular typescript rxjs

在 Angular2 中。我想为求职者加载 2 组不同的路由模块,1 为雇主加载。网址相同,但求职者和雇主的模块会有所不同。因此,我想根据登录 session 动态加载路由模块。有什么办法可以做到吗??或者有没有更好的方法可以在 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 对象。

(有没有更好的方法实现根据用户session加载不同路由的特性)

最佳答案

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

关于javascript - 在 Angular2 NGmodule 中。我想根据条件加载 2 组不同的路由模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40573539/

相关文章:

angular - ASP.NET Core 2.1 Angular 6.0 SPA 模板 - 用于索引而不是静态页面的 Razor 页面 (cshtml)

javascript - 当我运行应用程序时,哪个文件首先在 angular 4 应用程序中运行?

reactjs - TypeScript Rest Props - 允许任何 Prop 或告诉组件它们将成为哪些 Prop

typescript - 如何使用 typescript 获取 `jsonwebtoken` 的 token 过期

javascript - 如何防止 Angular 8 中的事件冒泡?

javascript - 阅读 Angular ui Bootstrap 轮播上的事件幻灯片#

javascript - 为什么 JavaScript 中 String.indexOf 检查失败?

angularjs - 单个数组 JSON 对象不会打印 (Ionic 2)

单击浏览器后退/前进时的 Angular 更新内容

javascript - 我网站中的恶意 javascript 代码