function
Injects a token from the currently active injector. inject
is only supported during instantiation of a dependency by the DI system. It can be used during:
constructor
) of a class being instantiated by the DI system, such as an @Injectable
or @Component
.useFactory
of a Provider
or an @Injectable
.factory
function specified for an InjectionToken
.inject<T>(token: ProviderToken<T>, flags: InjectOptions | InjectFlags = InjectFlags.Default): T | null
token | ProviderToken<T> | A token that represents a dependency that should be injected. |
flags | InjectOptions | InjectFlags | Optional flags that control how injection is executed. The flags correspond to injection strategies that can be specified with parameter decorators Optional. Default is |
T | null
: the injected value if operation is successful, null
otherwise.
Error
if called outside of a supported context.
|
token | ProviderToken<T> | A token that represents a dependency that should be injected. |
T
: the injected value if operation is successful, null
otherwise.
Error
if called outside of a supported context.
inject(token: ProviderToken<T>, flags?: InjectFlags): T | null
Deprecated prefer an options object instead of InjectFlags
token | ProviderToken<T> | A token that represents a dependency that should be injected. |
flags | InjectFlags | Control how injection is executed. The flags correspond to injection strategies that can be specified with parameter decorators Optional. Default is |
T | null
: the injected value if operation is successful, null
otherwise.
Error
if called outside of a supported context.
inject(token: ProviderToken<T>, options: InjectOptions & { optional?: false; }): T
token | ProviderToken<T> | A token that represents a dependency that should be injected. |
options | InjectOptions & { optional?: false; } | Control how injection is executed. Options correspond to injection strategies that can be specified with parameter decorators |
T
: the injected value if operation is successful.
Error
if called outside of a supported context, or if the token is not found.
inject(token: ProviderToken<T>, options: InjectOptions): T | null
token | ProviderToken<T> | A token that represents a dependency that should be injected. |
options | InjectOptions | Control how injection is executed. Options correspond to injection strategies that can be specified with parameter decorators |
T | null
: the injected value if operation is successful, null
if the token is not found and optional injection has been requested.
Error
if called outside of a supported context, or if the token is not found and optional injection was not requested.
In practice the inject()
calls are allowed in a constructor, a constructor parameter and a field initializer:
@Injectable({providedIn: 'root'}) export class Car { radio: Radio|undefined; // OK: field initializer spareTyre = inject(Tyre); constructor() { // OK: constructor body this.radio = inject(Radio); } }
It is also legal to call inject
from a provider's factory:
providers: [ {provide: Car, useFactory: () => { // OK: a class factory const engine = inject(Engine); return new Car(engine); }} ]
Calls to the inject()
function outside of the class creation context will result in error. Most notably, calls to inject()
are disallowed after a class instance was created, in methods (including lifecycle hooks):
@Component({ ... }) export class CarComponent { ngOnInit() { // ERROR: too late, the component instance was already created const engine = inject(Engine); engine.start(); } }
© 2010–2023 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://angular.io/api/core/inject