forRoot() and forChild() are Angular module configuration methods that help manage singleton services and routing across root and feature modules. forRoot() configures global providers and routes for the root module, while forChild() configures additional routes for feature modules without duplicating providers.
What are Angular modules’ forRoot() and forChild() methods used for?
Use guided tracks for structured prep, then practice company-specific question sets when you want targeted interview coverage.
Overview
In Angular, modules can include routing and services. The forRoot() and forChild() methods are conventions that help manage how routes and services are provided across different parts of an application — ensuring that global providers are instantiated only once, and feature modules get their own route configurations.
The Problem They Solve
When you import the same module (such as a routing or service module) into multiple feature modules, Angular might create multiple instances of services or conflicting route definitions. forRoot() and forChild() prevent this by clearly defining which module provides shared singletons (root-level) and which only extends configuration (child-level).
forRoot()
The forRoot() method is used in the root module (usually AppModule). It configures and provides application-wide services, singletons, and routes that should be initialized once for the entire application.
// Example: AppRoutingModule with forRoot()
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './home/home.component';
const routes: Routes = [
{ path: '', component: HomeComponent }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}
Here, RouterModule.forRoot() sets up the application's root router. It defines the initial navigation paths and ensures that only one router service instance exists across the app.
forChild()
The forChild() method is used in feature modules. It allows these modules to define their own route configurations without re-initializing the router or global providers. This supports modular architecture and lazy loading.
// Example: ProductsRoutingModule with forChild()
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { ProductListComponent } from './product-list/product-list.component';
import { ProductDetailComponent } from './product-detail/product-detail.component';
const routes: Routes = [
{ path: '', component: ProductListComponent },
{ path: ':id', component: ProductDetailComponent }
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class ProductsRoutingModule {}
Here, RouterModule.forChild() defines routes specific to the ProductsModule. It does not create a new router instance but instead extends the main router configuration.
forRoot() and forChild() in Shared Modules
Custom shared modules (e.g., logging or analytics) may also use the forRoot()/forChild() pattern to manage singleton service instantiation.
// Example: SharedModule using forRoot()
@NgModule({
providers: []
})
export class SharedModule {
static forRoot(): ModuleWithProviders<SharedModule> {
return {
ngModule: SharedModule,
providers: [LoggingService] // Singleton provider
};
}
}
This ensures that the LoggingService is instantiated only once at the root level, even if SharedModule is imported in multiple places.
Method | Used In | Purpose | Example |
|---|---|---|---|
forRoot() | Root module (AppModule) | Configures global services and root routing | RouterModule.forRoot(routes) |
forChild() | Feature module | Defines module-specific routes without creating new instances | RouterModule.forChild(routes) |
Best Practices
- Use
forRoot()only once — in the root module. - Use
forChild()in feature or lazy-loaded modules. - Do not import
RouterModule.forRoot()in feature modules, as it can cause multiple router instances and unpredictable behavior.
Think of forRoot() as setting up the main roads of your app, while forChild() adds local streets within neighborhoods (feature modules) without rebuilding the entire map.
forRoot()initializes global providers and routing — used in the root module.forChild()defines feature module routes without duplicating providers.- Together, they ensure scalable, modular, and efficient routing and service management in Angular applications.