Understanding Angular RouterModule: forRoot vs forChild

Dec 06, 2025 · Programming · 10 views · 7.8

Keywords: Angular | RouterModule | forRoot | forChild | Lazy Loading

Abstract: This article explores the differences between RouterModule.forRoot and RouterModule.forChild in Angular, explaining their use cases in eager and lazy loaded modules. It covers the underlying dependency injection mechanisms and provides practical examples to help developers avoid common confusions.

Introduction

In Angular, routing is managed by the RouterModule. Two key methods, forRoot and forChild, are used to configure routes, but they serve different purposes.

Core Concepts

RouterModule.forRoot is used for the root module, creating a module that includes the router service, all directives, and the specified routes. This ensures the router service is available as a singleton throughout the application.

RouterModule.forChild is used for child modules, creating a module that includes directives and the specified routes, but not the router service. This is suitable for lazy loaded modules, as the router service is already provided from the root module.

Dependency Injection and Module Mechanism

Angular uses dependency injection to manage services. The root injector provides services for the entire app, while lazy loaded modules have their own child injector. When modules are imported via <code>imports</code>, providers are added to the injector. To differentiate service instances, RouterModule uses the <code>ROUTES</code> token to manage route configurations.

Code Examples

Here is an example demonstrating how to use forRoot and forChild in root and lazy loaded modules.

// Root module configuration
import { RouterModule, Routes } from '@angular/router';

const routes: Routes = [
  { path: 'home', component: HomeComponent }
];

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

For lazy loaded modules, use forChild to add specific routes without duplicating the router service.

// Lazy loaded module configuration
import { RouterModule, Routes } from '@angular/router';

const childRoutes: Routes = [
  { path: 'profile', component: ProfileComponent }
];

@NgModule({
  imports: [RouterModule.forChild(childRoutes)],
  exports: [RouterModule]
})
export class LazyModule { }

Use Cases

forRoot should be applied in the app root module to ensure global router service. forChild is used in feature modules, especially lazy loaded ones, to extend routes without introducing redundant services.

Conclusion

Understanding the differences between forRoot and forChild is crucial for optimizing Angular application structure. It helps manage route configurations, avoid service duplication, and improve modularity.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.