Explain the purpose of RouterModule.forRoot() method

Angular’s routing module plays an important role in creating single-page applications (SPAs) by enabling navigation between different views or components. Among the key methods provided by the RouterModule, forRoot() stands out as a fundamental piece in configuring routing within an Angular application. In this article, we will see the purpose and significance of the forRoot() method in Angular routing.

What is forRoot()?

The forRoot() method is a static method provided by the RouterModule in Angular. It is typically called within the AppRoutingModule or any other module that serves as the root module of the application. This method is used to configure the router with routes and navigation-related settings at the application’s root level.

Syntax:

import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { appRoutes } from './app.routes'; // Importing application routes

@NgModule({
imports: [RouterModule.forRoot(appRoutes)],
exports: [RouterModule]
})
export class AppRoutingModule { }

In this example for syntax, appRoutes contains the route configuration for the application, and RouterModule.forRoot() is used to configure the router with these routes at the root level of the application. This sets the stage for efficient routing and navigation throughout the Angular application.

Features of RouterModule.forRoot()

  • Route Configuration: forRoot() method allows to define the initial route configuration for the application. This includes specifying the routes, their corresponding components, and any additional route-related settings such as route guards or resolvers.
  • Router Initialization: It initializes the router service and sets up the router environment for the entire application. This includes creating the router singleton instance and providing it to the application-wide dependency injection tree.
  • Router Settings: The method can accept an optional parameter for configuring the router behavior and settings. These settings include options like the initial navigation strategy, whether to use hash-based or path-based routing, and more.

Purpose of RouterModule.forRoot()

The primary purpose of RouterModule.forRoot() method is to set up the router configuration and environment at the root level of the Angular application. This method performs several crucial tasks essential for proper routing functionality:

  • Initializing Router: forRoot() initializes the Angular router service, creating a singleton instance of the router that can be injected and used throughout the application.
  • Configuring Routes: It allows developers to define the initial route configuration for the application. This includes specifying the routes and their corresponding components, as well as any additional route-related settings.
  • Setting Up Router Environment: The method sets up the router environment, including configuring navigation strategies, route matching algorithms, and other settings that affect how routing behaves within the application.

Steps to Create Angular Application

Step 1: Create a new Angular project

Open your terminal or command prompt and run the following command to create a new Angular project:

ng new router-module

Step 2: Generate a module using the following command.

ng generate component home
ng generate component about
ng generate component contact

Folder Structure:

Dependencies:

"dependencies": {
"@angular/animations": "^17.3.0",
"@angular/common": "^17.3.0",
"@angular/compiler": "^17.3.0",
"@angular/core": "^17.3.0",
"@angular/forms": "^17.3.0",
"@angular/platform-browser": "^17.3.0",
"@angular/platform-browser-dynamic": "^17.3.0",
"@angular/router": "^17.3.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.14.3"
},

Step 4: Create the required files as shown in folder structure and add the following codes.

Example:

HTML
<!--app.component.html-->

<h1>My Angular App</h1>
<router-outlet></router-outlet>
HTML
<!-- app.home.html --> 

<p>home works!</p>
HTML
<!-- app.about.html -->

<p>about works!</p>
HTML
<!-- app.contact.html -->

<p>contact works!</p>
JavaScript
//app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';
import { ContactComponent } from './contact/contact.component';

@NgModule({
    declarations: [
        AppComponent,
        HomeComponent,
        AboutComponent,
        ContactComponent
    ],
    imports: [
        BrowserModule,
        AppRoutingModule
    ],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule { }
JavaScript
//src/app/app-routing.module.ts

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';
import { ContactComponent } from './contact/contact.component';

const routes: Routes = [
    { path: '', component: HomeComponent },
    { path: 'about', component: AboutComponent },
    { path: 'contact', component: ContactComponent },
];

@NgModule({
    imports: [RouterModule.forRoot(routes)],
    exports: [RouterModule]
})
export class AppRoutingModule { }

Step 5: Run your Angular application

Now, you can run your Angular application:

ng serve

Output:



Contact Us