填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写Home

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

静态方法

static create(providers: StaticProvider[], parent?: Injector): Injector
      
      static create(providers: StaticProvider[], parent?: Injector): Injector
    

Deprecated from v5 use the new signature Injector.create(options)

参数
providers StaticProvider[]
parent Injector

可选. 默认值是 undefined.

返回值

Injector

Creates a new injector instance that provides one or more dependencies, according to a given type or types of StaticProvider.

static create(options: { providers: StaticProvider[]; parent?: Injector; name?: string; }): Injector
      
      static create(options: { providers: StaticProvider[]; parent?: Injector; name?: string; }): Injector
    
参数
options object

An object with the following properties:

  • providers: An array of providers of the StaticProvider type.
  • parent: (optional) A parent injector.
  • name: (optional) A developer-defined identifying name for the new injector.
返回值

Injector: The new injector instance.

方法

Retrieves an instance from the injector based on the provided token.

abstract get<T>(token: Type<T> | InjectionToken<T> | AbstractType<T>, notFoundValue?: T, flags?: InjectFlags): T
      
      abstract get<T>(token: Type<T> | InjectionToken<T> | AbstractType<T>, notFoundValue?: T, flags?: InjectFlags): T
    
参数
token Type | InjectionToken | AbstractType
notFoundValue T

可选. 默认值是 undefined.

flags InjectFlags

可选. 默认值是 undefined.

返回值

T: The instance from the injector if defined, otherwise the notFoundValue.

异常

错误 When the notFoundValue is undefined or Injector.THROW_IF_NOT_FOUND.

abstract get(token: any, notFoundValue?: any): any
      
      abstract get(token: any, notFoundValue?: any): any
    

Deprecated from v4.0.0 use Typeor InjectionToken

参数
token any
notFoundValue any

可选. 默认值是 undefined.

返回值

any

使用说明

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);
      
      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');
      
      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);
      
      const injector = Injector.create({providers: []});
expect(injector.get(Injector)).toBe(injector);