forRoot / forChild is a pattern for singleton services that most of us know from routing. Routing is actually the main use case for it and as it is not commonly used outside of it, I wouldn’t be surprised if most Angular developers haven’t given it a second thought. However, as the official Angular documentation puts it:

“Understanding how _forRoot()_ works to make sure a service is a singleton will inform your development at a deeper level.”

So let’s go.

Providers & Injectors

Angular comes with a dependency injection (DI) mechanism. When a component depends on a service, you don’t manually create an instance of the service. You inject the service and the dependency injection system takes care of providing an instance.

#dependency-injection #web-development #javascript #angular

Understand forRoot and forChild in Angular
15.50 GEEK