What is a Provider
A provider is a registration rule that tells the container how to resolve a token.
Why Providers Matter
- They make service graphs explicit.
- They enable local overrides in child modules.
- They improve testability by allowing token-level replacement.
Provider Forms
- Constructor provider (
MyService) useClass({ provide, useClass })useValue({ provide, useValue })useFactory({ provide, useFactory, inject? })useExisting({ provide, useExisting })
Scope Behavior
Scope controls instance lifetime:
singletontransientcontainerScopedresolutionScoped(exceptuseFactory)
Lifecycle and Providers
Providers that resolve to objects implementing:
onModuleInitonModuleMountonModuleUnmountonModuleDestroy
are automatically included in provider lifecycle execution for owned modules.
useExisting providers are excluded from lifecycle token collection to avoid duplicate lifecycle calls through alias tokens.
Where Providers Are Used
ModuleProvideracceptsprovidersand registers them in module scope.- Child modules can shadow parent tokens.