importProvidersFrom converts providers declared by one or more NgModules into providers usable by standalone Angular applications. It provides interoperability while older libraries migrate. section 2.3.2
Add HttpClient providers to app configuration
Place the function in the application configuration: providers: [provideRouter(routes), importProvidersFrom(HttpClientModule)]. Use it in the application injection context, such as bootstrapApplication configuration or route providers. section 2.3.2
Use importProvidersFrom when:
- A dependency still comes from an NgModule without a standalone API. section 2.3.2
- A third-party Angular library has not migrated to standalone APIs. section 2.3.2
- A standalone application needs providers extracted from multiple modules. section 2.3.2
Table 2.1 Different scenarios of providing dependencies
|
Question
|
Answer without standalone APIs
|
Answer with standalone APIs
|
|---|---|---|
|
Do I need to provide every dependency in every component?
|
Either this or mark the service as
providedIn:
'root'.
|
We can use a standalone API if one is provided.
|
|
How do I import services that are provided in other modules (without special standalone APIs)?
|
Import the module directly into the component.
|
Use the
importProvidersFrom function in the application main.ts file.
|
|
How do I import built-in Angular dependencies?
|
Import the whole module.
|
Use existing standalone APIs.
|
Compare module-based dependency provisioning with standalone APIs and importProvidersFrom. section 2.3.2
