Angular & Signals (283 Qs)•Part 9: NgModules, Providers, NgZone & Injector Hierarchies (213 - 255)
How does forRoot method helpful to avoid duplicate router instances?
If the RouterModule module didn’t have forRoot() static method then each feature module would instantiate a new Router instance, which leads to broken application due to duplicate instances. After using forRoot() method, the root application module imports RouterModule.forRoot(...) and gets a Router, and all feature modules import RouterModule.forChild(...) which does not instantiate another Router.
Practical Code Example
typescript
// forRoot() pattern:
// Provides routes + router services as singletons in AppModule;
// forChild() provides additional child routes without re-providing router services:
// RouterModule.forRoot(routes) in AppModule
// RouterModule.forChild(childRoutes) in FeatureModule