In Angular2, I am looking to load two different sets of route modules - one for jobseekers and the other for employers. Both sets will have the same URLs but will contain different modules for jobseekers and employers. Therefore, I need a way to dynamically load the route modules based on the user's login session. Is there a method available to achieve this, or is there a more efficient way to implement it in Angular2?
Essentially, I want the ability to load ngModules conditionally.
Here is an example of the code snippet:
@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) {
}
// Custom Route
export class customRoutes {
public customRoutes;
constructor(@Inject(AccountService) accountService:AccountService) {
}
getCustomRoutes() {
return this.customRoutes = (accountService.getCheckEmployer()) ? JobSeekerRoutes : EmployerRoutes;
}
}
// However, getCustomRoutes method needs to be static to be called in the NgModule. But making it static would restrict access to the accountService object.
(Is there a better approach to implementing the feature of loading different routes based on the user's session?)