填寫這份《一分鐘調查》,幫我們(開發組)做得更好!去填寫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();