Injector
Concrete injectors implement this interface. Injectors are configured with providers that associate dependencies of various types with injection tokens.
abstract class Injector {
static THROW_IF_NOT_FOUND: THROW_IF_NOT_FOUND
static NULL: Injector
static create(options: StaticProvider[] | { providers: (any[] | TypeProvider | ValueProvider | ClassProvider | ConstructorProvider | ExistingProvider | FactoryProvider | StaticClassProvider)[]; parent?: Injector; name?: string; }, parent?: Injector): Injector
abstract get<T>(token: ProviderToken<T>, notFoundValue: undefined, options: InjectOptions & { optional?: false; }): T
abstract get<T>(token: ProviderToken<T>, notFoundValue: null, options: InjectOptions): T | null
abstract get<T>(token: ProviderToken<T>, notFoundValue?: T, options?: InjectOptions | InjectFlags): T
abstract get<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): T
abstract get(token: any, notFoundValue?: any): any
}
Subclasses
See also
Provided in
-
'any'
Static properties
Property | Description |
---|---|
static THROW_IF_NOT_FOUND: THROW_IF_NOT_FOUND
|
|
static NULL: Injector
|
Static methods
create() | ||||||
---|---|---|---|---|---|---|
Deprecated from v5 use the new signature Injector.create(options) Parameters
Returns |
||||||
Creates a new injector instance that provides one or more dependencies,
according to a given type or types of
Parameters
Returns
|
Methods
get() | ||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Retrieves an instance from the injector based on the provided token. |
||||||||||||||||||||||||||||||||||||
|
token
|
ProviderToken<T> |
|
notFoundValue
|
undefined |
|
options
|
InjectOptions & { optional?: false; } |
Returns
T
: The instance from the injector if defined, otherwise the notFoundValue
.
Throws
Error
When the notFoundValue
is undefined
or Injector.THROW_IF_NOT_FOUND
.
Overload #1
abstract get<T>(token: ProviderToken<T>, notFoundValue: null, options: InjectOptions): T | null
Parameters
token
|
ProviderToken<T> |
|
notFoundValue
|
null |
|
options
|
InjectOptions |
Returns
T | null
: The instance from the injector if defined, otherwise the notFoundValue
.
Throws
Error
When the notFoundValue
is undefined
or Injector.THROW_IF_NOT_FOUND
.
Overload #2
abstract get<T>(token: ProviderToken<T>, notFoundValue?: T, options?: InjectOptions | InjectFlags): T
Parameters
token
|
ProviderToken<T> |
|
notFoundValue
|
T |
Optional. Default is |
options
|
InjectOptions | InjectFlags |
Optional. Default is |
Returns
T
: The instance from the injector if defined, otherwise the notFoundValue
.
Throws
Error
When the notFoundValue
is undefined
or Injector.THROW_IF_NOT_FOUND
.
Overload #3
abstract get<T>(token: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): T
Deprecated use object-based flags (InjectOptions
) instead.
Parameters
token
|
ProviderToken<T> |
|
notFoundValue
|
T |
Optional. Default is |
flags
|
InjectFlags |
Optional. Default is |
Returns
T
: The instance from the injector if defined, otherwise the notFoundValue
.
Throws
Error
When the notFoundValue
is undefined
or Injector.THROW_IF_NOT_FOUND
.
Overload #4
Usage notes
The following example creates a service injector instance.
class Square {
name = 'square';
}
const injector = Injector.create({providers: [{provide: Square, deps: []}]});
const shape: Square = injector.get(Square);
expect(shape.name).toEqual('square');
expect(shape instanceof Square).toBe(true);
Usage example
const injector: Injector = Injector.create({
providers: [{provide: 'validToken', useValue: 'Value'}],
});
expect(injector.get('validToken')).toEqual('Value');
expect(() => injector.get('invalidToken')).toThrowError();
expect(injector.get('invalidToken', 'notFound')).toEqual('notFound');
Injector
returns itself when given Injector
as a token:
const injector = Injector.create({providers: []});
expect(injector.get(Injector)).toBe(injector);