liveBook · Manning

RouterModule.forChild

As explained in Pro Angular 16

RouterModule.forChild is an Angular API method that creates router configuration for routes declared by a feature module without providing the root routing service.

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

How Pro Angular 16 covers it

How deep it goes
Explains service creation, configuration merging, module imports, and a complete feature-module listing, but not router internals.
Examples
Uses an AdminModule with auth, main, and wildcard redirect routes, plus an on-demand module with an empty-path component route.
What it assumes
Assumes familiarity with Angular modules, components, route definitions, and imports while distinguishing forChild from forRoot.
What you can do after
Apply via Angular Router by defining feature routes, importing the resulting configuration, and loading the module through application navigation.
How it connects
Connects feature modules with Angular Router, lazy loading, child routes, route guards, redirects, and component declarations.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro Angular 16. The section links point to the parts of the book it draws on.