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

Self

将在构造函数参数上使用参数装饰器,该装饰器告诉 DI 框架从本地注入器开始解析依赖项。

Parameter decorator to be used on constructor parameters, which tells the DI framework to start dependency resolution from the local injector.

查看"说明"...

参见

说明

解析器在注入器层次结构中向上查找,因此此类的子级必须配置其自己的提供者或为空结果做好准备。

Resolution works upward through the injector hierarchy, so the children of this class must configure their own providers or be prepared for a null result.

Further information available in the Usage Notes...

选项

使用说明

在以下示例中,依赖关系可以在实例化类本身时由本地注入器解析,而在实例化子代时不能解析。

In the following example, the dependency can be resolved by the local injector when instantiating the class itself, but not when instantiating a child.

      
      class Dependency {}

@Injectable()
class NeedsDependency {
  constructor(@Self() public dependency: Dependency) {}
}

let inj = Injector.create({
  providers: [
    {provide: Dependency, deps: []},
    {provide: NeedsDependency, deps: [[new Self(), Dependency]]}
  ]
});
const nd = inj.get(NeedsDependency);

expect(nd.dependency instanceof Dependency).toBe(true);

const child = Injector.create({
  providers: [{provide: NeedsDependency, deps: [[new Self(), Dependency]]}],
  parent: inj
});
expect(() => child.get(NeedsDependency)).toThrowError();