在 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/