Injector
Concrete injectors implement this interface.
abstract class Injector {
static THROW_IF_NOT_FOUND: _THROW_IF_NOT_FOUND
static NULL: Injector
static ngInjectableDef: defineInjectable({...})
static create(options: StaticProvider[] | { providers: StaticProvider[]; parent?: Injector; name?: string; }, parent?: Injector): Injector
abstract get<T>(token: Type<T> | InjectionToken<T>, notFoundValue?: T, flags?: InjectFlags): T
}
说明
For more details, see the "Dependency Injection Guide".
静态属性
属性 | 说明 |
---|---|
static THROW_IF_NOT_FOUND: _THROW_IF_NOT_FOUND | |
static NULL: Injector | |
static ngInjectableDef: defineInjectable({
providedIn: 'any' as any,
factory: () => inject(INJECTOR)
}) |
静态方法
Create a new Injector which is configure using | ||||||
Deprecated from v5 use the new signature Injector.create(options) 参数
返回值 | ||||||
使用说明Example
|
方法
Retrieves an instance from the injector based on the provided token. | |||||||||
参数
返回值
异常
| |||||||||
使用说明
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);