填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫Home

ReflectiveInjector

一個 ReflectiveDependency 注入容器,用於實例化物件和解析依賴關係。

A ReflectiveDependency injection container used for instantiating objects and resolving dependencies.

檢視"說明"...

已棄用: from v5 - slow and brings in a lot of code, Use Injector.create instead.

從 v5 開始 - 速度慢,並且引入了大量程式碼,請改用 Injector.create

      
      abstract class ReflectiveInjector implements Injector {
  static resolve(providers: Provider[]): ResolvedReflectiveProvider[]
  static resolveAndCreate(providers: Provider[], parent?: Injector): ReflectiveInjector
  static fromResolvedProviders(providers: ResolvedReflectiveProvider[], parent?: Injector): ReflectiveInjector
  abstract parent: Injector | null
  abstract resolveAndCreateChild(providers: Provider[]): ReflectiveInjector
  abstract createChildFromResolved(providers: ResolvedReflectiveProvider[]): ReflectiveInjector
  abstract resolveAndInstantiate(provider: Provider): any
  abstract instantiateResolved(provider: ResolvedReflectiveProvider): any
  abstract get(token: any, notFoundValue?: any): any
}
    

說明

Injector 替代了 new 運算子,該運算子可以自動解析建構函式的依賴關係。

An Injector is a replacement for a new operator, which can automatically resolve the constructor dependencies.

在典型的用法中,應用程式程式碼會在建構函式中要求依賴項,並由 Injector 進行解析。

In typical use, application code asks for the dependencies in the constructor and they are resolved by the Injector.

Further information available in the Usage Notes...

靜態方法

將一組提供者定義轉換為一組已解析的提供者。

Turns an array of provider definitions into an array of resolved providers.

      
      static resolve(providers: Provider[]): ResolvedReflectiveProvider[]
    
引數
providers Provider[]
返回值

ResolvedReflectiveProvider[]

解析是展平多個巢狀陣列並將其各個提供者轉換為 ResolvedReflectiveProvider 陣列的過程。

A resolution is a process of flattening multiple nested arrays and converting individual providers into an array of ResolvedReflectiveProviders.

使用說明

例子
Example
      
      @Injectable()
class Engine {
}

@Injectable()
class Car {
  constructor(public engine:Engine) {}
}

var providers = ReflectiveInjector.resolve([Car, [[Engine]]]);

expect(providers.length).toEqual(2);

expect(providers[0] instanceof ResolvedReflectiveProvider).toBe(true);
expect(providers[0].key.displayName).toBe("Car");
expect(providers[0].dependencies.length).toEqual(1);
expect(providers[0].factory).toBeDefined();

expect(providers[1].key.displayName).toBe("Engine");
});
    

解析供應商陣列,並從這些供應商建立注入器。

Resolves an array of providers and creates an injector from those providers.

      
      static resolveAndCreate(providers: Provider[], parent?: Injector): ReflectiveInjector
    
引數
providers Provider[]
parent Injector
可選. 預設值是 `undefined`.
返回值

ReflectiveInjector

傳入的提供者可以是 TypeProvider 或更多提供者的遞迴陣列。

The passed-in providers can be an array of Type, Provider, or a recursive array of more providers.

使用說明

例子
Example
      
      @Injectable()
class Engine {
}

@Injectable()
class Car {
  constructor(public engine:Engine) {}
}

var injector = ReflectiveInjector.resolveAndCreate([Car, Engine]);
expect(injector.get(Car) instanceof Car).toBe(true);
    

從先前解析的提供者建立注入器。

Creates an injector from previously resolved providers.

      
      static fromResolvedProviders(providers: ResolvedReflectiveProvider[], parent?: Injector): ReflectiveInjector
    
引數
providers ResolvedReflectiveProvider[]
parent Injector
可選. 預設值是 `undefined`.
返回值

ReflectiveInjector

建議使用此 API 在對效能敏感的部分建構注入器。

This API is the recommended way to construct injectors in performance-sensitive parts.

使用說明

例子
Example
      
      @Injectable()
class Engine {
}

@Injectable()
class Car {
  constructor(public engine:Engine) {}
}

var providers = ReflectiveInjector.resolve([Car, Engine]);
var injector = ReflectiveInjector.fromResolvedProviders(providers);
expect(injector.get(Car) instanceof Car).toBe(true);
    

屬性

屬性說明
abstract parent: Injector | null唯讀

此注入器的父代。

Parent of this injector.

方法

解析一組提供者,並從這些提供者建立子注入器。

Resolves an array of providers and creates a child injector from those providers.

      
      abstract resolveAndCreateChild(providers: Provider[]): ReflectiveInjector
    
引數
providers Provider[]
返回值

ReflectiveInjector

傳入的提供者可以是 TypeProvider 或更多提供者的遞迴陣列。

The passed-in providers can be an array of Type, Provider, or a recursive array of more providers.

使用說明

例子
Example
      
      class ParentProvider {}
class ChildProvider {}

var parent = ReflectiveInjector.resolveAndCreate([ParentProvider]);
var child = parent.resolveAndCreateChild([ChildProvider]);

expect(child.get(ParentProvider) instanceof ParentProvider).toBe(true);
expect(child.get(ChildProvider) instanceof ChildProvider).toBe(true);
expect(child.get(ParentProvider)).toBe(parent.get(ParentProvider));
    

從先前解析的提供者中建立子注入器。

Creates a child injector from previously resolved providers.

      
      abstract createChildFromResolved(providers: ResolvedReflectiveProvider[]): ReflectiveInjector
    
引數
providers ResolvedReflectiveProvider[]
返回值

ReflectiveInjector

建議使用此 API 在對效能敏感的部分構造注入器。

This API is the recommended way to construct injectors in performance-sensitive parts.

使用說明

例子
Example
      
      class ParentProvider {}
class ChildProvider {}

var parentProviders = ReflectiveInjector.resolve([ParentProvider]);
var childProviders = ReflectiveInjector.resolve([ChildProvider]);

var parent = ReflectiveInjector.fromResolvedProviders(parentProviders);
var child = parent.createChildFromResolved(childProviders);

expect(child.get(ParentProvider) instanceof ParentProvider).toBe(true);
expect(child.get(ChildProvider) instanceof ChildProvider).toBe(true);
expect(child.get(ParentProvider)).toBe(parent.get(ParentProvider));
    

解析提供者並在注入器的上下文中實例化物件。

Resolves a provider and instantiates an object in the context of the injector.

      
      abstract resolveAndInstantiate(provider: Provider): any
    
引數
provider Provider
返回值

any

注入器不會快取建立的物件。

The created object does not get cached by the injector.

使用說明

例子
Example
      
      @Injectable()
class Engine {
}

@Injectable()
class Car {
  constructor(public engine:Engine) {}
}

var injector = ReflectiveInjector.resolveAndCreate([Engine]);

var car = injector.resolveAndInstantiate(Car);
expect(car.engine).toBe(injector.get(Engine));
expect(car).not.toBe(injector.resolveAndInstantiate(Car));
    

在注入器的上下文中使用解析的提供者實例化物件。

Instantiates an object using a resolved provider in the context of the injector.

      
      abstract instantiateResolved(provider: ResolvedReflectiveProvider): any
    
引數
provider ResolvedReflectiveProvider
返回值

any

注入器不會快取建立的物件。

The created object does not get cached by the injector.

使用說明

例子
Example
      
      @Injectable()
class Engine {
}

@Injectable()
class Car {
  constructor(public engine:Engine) {}
}

var injector = ReflectiveInjector.resolveAndCreate([Engine]);
var carProvider = ReflectiveInjector.resolve([Car])[0];
var car = injector.instantiateResolved(carProvider);
expect(car.engine).toBe(injector.get(Engine));
expect(car).not.toBe(injector.instantiateResolved(carProvider));
    
      
      abstract get(token: any, notFoundValue?: any): any
    
引數
token any
notFoundValue any
可選. 預設值是 `undefined`.
返回值

any

使用說明

例子

Example

以下示例建立一個配置為建立 EngineCarInjector

The following example creates an Injector configured to create Engine and Car.

      
      @Injectable()
class Engine {
}

@Injectable()
class Car {
  constructor(public engine:Engine) {}
}

var injector = ReflectiveInjector.resolveAndCreate([Car, Engine]);
var car = injector.get(Car);
expect(car instanceof Car).toBe(true);
expect(car.engine instanceof Engine).toBe(true);
    

Notice, we don't use the new operator because we explicitly want to have the Injector resolve all of the object's dependencies automatically.