填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫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: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): T
}
    

參見

Provided in

  •       
          'any'
        

靜態屬性

屬性說明
static THROW_IF_NOT_FOUND: THROW_IF_NOT_FOUND
static NULL: Injector

靜態方法

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

從 v5 開始使用新的簽名 Injector.create(options)

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

引數
providers StaticProvider[]
parent Injector
可選. 預設值是 `undefined`.
返回值

Injector

建立一個新的注入器實例,該實例會根據指定的型別或 StaticProvider 的型別提供一個或多個依賴項。

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
    
引數
options object

具有以下屬性的物件:

An object with the following properties:

  • name :(可選)新注入器的開發人員自訂的標識名稱。

    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: ProviderToken<T>, notFoundValue?: T, flags?: InjectFlags): T
    
引數
token ProviderToken
notFoundValue T
可選. 預設值是 `undefined`.
flags InjectFlags
可選. 預設值是 `undefined`.
返回值

注入器的實例(如果已定義),否則為 notFoundValue

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

異常

錯誤 When the notFoundValue is undefined or Injector.THROW_IF_NOT_FOUND.

notFoundValueundefinedInjector.THROW_IF_NOT_FOUND 時。

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

從 v4.0.0 開始,改用 Type、AbstractType或 InjectionToken

Deprecated from v4.0.0 use ProviderToken

引數
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);
    

使用範例

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);