Within Angular2 NGmodule, I aim to dynamically load two distinct sets of route modules depending on certain conditions

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?)

Answer ā„–1

Currently, dynamically setting routes is not a feature that is available. Routes must be defined during the initialization of the Angular application and cannot be altered afterwards.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The module 'three/examples/jsm/controls/OrbitControls' or its corresponding type declarations could not be located

In my React project, I am utilizing TypeScript and three.js, where I'm importing OrbitControls in the following manner: import * as THREE from "three"; import { OrbitControls } from "three/examples/jsm/controls/OrbitControls"; How ...

Tips for properly setting and accessing data objects in React when utilizing a Firebase database

Iā€™m encountering issues with storing and retrieving data using the useState hook in React after fetching it from a Firebase database. The issue appears to be related to not all users having all values in the playerData data packet ā€“ for example, if a p ...

Having trouble with res.render() when making an axios request?

I am encountering an issue with my axios requests. I have two requests set up: one to retrieve data from the API and another to send this data to a view route. const response = await axios({ method: 'POST', url: 'http:// ...

Exploring Google Places with Angular 2, Typescript, and Ionic 2

Has anyone successfully transformed this code into TypeScript? var map; var infowindow; function initMap() { var pyrmont = {lat: -33.867, lng: 151.195}; map = new google.maps.Map(document.getElementById('map'), { center: py ...

React with TypeScript presents an unusual "Unexpected token parsing error"

Recently, I encountered an issue with my helper function that was originally written in JavaScript and was functioning perfectly. However, as soon as I introduced TypeScript into the mix, strange behaviors started to surface. Here is the snippet of code fr ...

What are the semantics behind implementing Basic Authentication in AngularJS and Spring Boot?

Currently going through a tutorial that involves AngularJS and Spring Security. The AngularJS navigation controller triggers an authenticate function upon page load: var authenticate = function(credentials, callback) { var headers = credentials ? { ...

Stop the stream coming from getUserMedia

After successfully channeling the stream from getUserMedia to a <video> element on the HTML page, I am able to view the video in that element. However, I have encountered an issue where if I pause the video using the controls on the video element a ...

Is there a way to switch tabs through programming?

Is there a way to switch between tabs using jQuery or JavaScript when using the tabbed content from ? I have tried using $("tab1").click(); I have created a fiddle for this specific issue which can be found at: https://jsfiddle.net/6e3y9663/1/ ...

Is the DOMContentLoaded event connected to the creation of the DOM tree or the rendering tree?

After profiling my app, I noticed that the event is triggered after 1.5 seconds, but the first pixels appear on the screen much later. It seems like the event may only relate to DOM tree construction. However, this tutorial has left me feeling slightly con ...

How to implement a form in PHP that doesn't refresh the page upon submission

I am having an issue with this ajax code. I know it works, but for some reason it's not submitting. <script type="text/javascript"> $(function(){ $('input[type=submit]').click(function(){ $.ajax({ type: "POST", ...

When an Angular service is created, its properties are not immediately configured

My current task involves testing an Angular (4.1) service that looks like this: @Injectable() export class JobService { private answerSource = new Subject<AnswerWrapper>(); answer$ = this.answerSource.asObservable(); answer(answer: AnswerWra ...

Node.js is encountering an undefined parameter in the function definition

I've encountered an issue while working on an application utilizing MongoDB as the database and node.js. I have a function that is supposed to receive a parameter named orderedBy, but when I console.log it, it appears as undefined. Can someone help me ...

How can I obtain the width of one element and then use it to adjust the size of another element that is

Currently, I am working on a dropdown menu and facing an issue with setting the submenus to match the width of the top-level page. In my HTML structure (specifically for a WordPress theme), it looks something like this: <ul class="menu"> <li ...

At what point is the ngOnInit function invoked?

Here is the code snippet I am working with: <div *ngIf="hotels$ | async as hotels; else loadGif "> <div *ngFor="let hotel of hotels | hotelsFilter: _filteredType; first as f; index as i " appInit [hotel]="hotel " [first]="f "> < ...

Firestore's get document method may cause an unmounted warning

I've been working on a React.js project that integrates with Firestore, and I ran into an issue where using the get method for fetching documents resulted in a "Can't perform a React state update on an unmounted component" warning. However, when ...

Is it feasible to verify for vacant dates with a single click?

Is there a way to determine if a date value is empty, and if it is, display a popup indicating so? After some research, I stumbled upon a similar issue where the date value was always filled with a default "mm/dd/yyyy" value. The solution provided involv ...

Is it possible for the app-routing.module.ts to have two paths with :/id?

When attempting to access the maindetail and childdetails pages using :/id, I encountered an issue on localhost where the desired card was not displaying on the maindetail page. The goal is to be able to click on the name "aniq" in the dashboard (image 1) ...

Unable to update array with HTTP Put request: No document was found that matches the criteria. The method 'send' is not available

I'm facing some challenges when trying to execute HTTP Put requests within an array using AngularJS and ExpressJS. The problem arises when I make the first HTTP Put call, everything works fine. However, on subsequent attempts, it fails to work. Below ...

Checking React props using Jest and Enzyme - A simple guide

Trying to understand React testing, I came across two files: Button.js and Button.test.js The code below contains the question along with comments: // Button.js import React from 'react'; import { string, bool, func } from 'prop-types&apos ...

A critical error has occurred: RangeError - The maximum call stack size has been exceeded while trying to

After attempting to filter a list of titles using Ng2SearchPipeModule, I imported the module in app.module.ts and created a new searchbar component. searchbar.component.ts import { FirebaseService } from './../../firebase.service'; import { Ang ...