【发布时间】: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