RouterModule.forChild creates routing configuration for a feature module without providing another root routing service. Angular merges that configuration into the application’s router when the module is loaded, making it suitable for feature modules, including dynamically loaded modules. section 7.1.1 section 27.3.2
Define routes inside a feature module
A feature module can package its supported URLs and components together: let routing = RouterModule.forChild([ { path: "auth", component: AuthComponent }, { path: "main", component: AdminComponent }, { path: "**", redirectTo: "auth" } ]); @NgModule({ imports: [CommonModule, FormsModule, routing], declarations: [AuthComponent, AdminComponent] }) export class AdminModule { } The routing object belongs in the module’s imports array. The feature remains self-contained: it declares its route URLs and displayed components while reusing dependencies from the main application when needed. section 7.1.1
Use forChild when:
- A feature module owns routes for its components. section 7.1.1
- A dynamically loaded module must provide routing information without creating a second router service. section 27.3.2
- The feature needs ordinary route capabilities such as child routes, guards, or redirects. section 27.3.2
