liveBook · Manning

importProvidersFrom

As explained in Modern Angular

importProvidersFrom is an Angular function that converts providers from NgModules or other provider sources into application or environment providers.

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

How Modern Angular covers it

How deep it goes
Explains provider extraction and application-context limits, stopping short of deeper dependency-injection internals.
Examples
Uses HttpClientModule in app.config.ts and contrasts it with the standalone provideHttpClient API.
What it assumes
Assumes familiarity with Angular providers, NgModules, standalone applications, and application configuration.
What you can do after
Operate/configure standalone applications with providers from legacy NgModules and third-party Angular libraries.
How it connects
Connects NgModules, standalone APIs, application configuration, bootstrapApplication, route providers, and dependency injection.

Related concepts

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