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. |
解析是展平多个嵌套数组并将其各个提供者转换为 A resolution is a process of flattening multiple nested arrays and converting individual providers into an array of |
使用说明例子Example
|
解析供应商数组,并从这些供应商创建注入器。 Resolves an array of providers and creates an injector from those providers. |
传入的提供者可以是 The passed-in providers can be an array of |
使用说明例子Example
|
从先前解析的提供者创建注入器。 Creates an injector from previously resolved providers. | ||||||
参数
返回值 | ||||||
建议使用此 API 在对性能敏感的部分构建注入器。 This API is the recommended way to construct injectors in performance-sensitive parts. | ||||||
使用说明例子Example
|
属性
属性 | 说明 |
---|---|
abstract parent: Injector | null | 只读 此注入器的父代。 Parent of this injector. |
方法
解析一组提供者,并从这些提供者创建子注入器。 Resolves an array of providers and creates a child injector from those providers. | |||
参数
返回值 | |||
传入的提供者可以是 The passed-in providers can be an array of | |||
使用说明例子Example
|
从先前解析的提供者中创建子注入器。 Creates a child injector from previously resolved providers. | |||
参数
返回值 | |||
建议使用此 API 在对性能敏感的部分构造注入器。 This API is the recommended way to construct injectors in performance-sensitive parts. | |||
使用说明例子Example
|
解析提供者并在注入器的上下文中实例化对象。 Resolves a provider and instantiates an object in the context of the injector. |
注入器不会缓存创建的对象。 The created object does not get cached by the injector. |
使用说明例子Example
|
在注入器的上下文中使用解析的提供者实例化对象。 Instantiates an object using a resolved provider in the context of the injector. | |||
参数
返回值
| |||
注入器不会缓存创建的对象。 The created object does not get cached by the injector. | |||
使用说明例子Example
|
使用说明
例子
Example
以下示例创建一个配置为创建 Engine
和 Car
的 Injector
。
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.