1

I've seen people using .forRoot() in their projects along with the main forRoot(paths inside...) which brings them to the sub-url.

What is the purpose of this empty for root?

Example (it is used in the imports of the defined Module):

    NgbModule.forRoot(),
    ShareButtonsModule.forRoot(),
    BrowserModule,
    HttpClientModule,
masterach
  • 437
  • 1
  • 6
  • 19

1 Answers1

4

forRoot is a convention for static class methods on modules.

It is used to keep certain providers of the module as singletons, meaning they are only injected into the main app module and not into individual components.

This way, you can use MyModule.forRoot() only in the main app module, and still import MyModule into components that need it.

The data that is passed inside of forRoot(...) depends on the module itself and the providers it has. Whether forRoot is empty or takes parameters, the point is that it is used only in the main app module.

Here is a good article discussing forRoot() in detail.

Michael Auderer
  • 507
  • 5
  • 12