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: StaticProvider[]; parent?: Injector; name?: string; }, parent?: Injector): Injector
abstract get<T>(token: Type<T> | InjectionToken<T> | AbstractType<T>, notFoundValue?: T, flags?: InjectFlags): T
}
参见
静态属性
属性 | 说明 |
---|---|
static THROW_IF_NOT_FOUND: THROW_IF_NOT_FOUND | |
static NULL: Injector |
静态方法
Deprecated from v5 use the new signature Injector.create(options) 参数
返回值 | ||||||
Creates a new injector instance that provides one or more dependencies, according to a given type or types of
参数
返回值
|
方法
Retrieves an instance from the injector based on the provided token. | |||||||||
参数
返回值
异常
| |||||||||
使用说明
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);