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

SkipSelf

將在建構函式引數上使用的引數裝飾器,該引數指示 DI 框架從父注入器啟動依賴項解析。解析器在注入器層次結構中向上查詢,因此不會檢查本地注入器的提供者。

Parameter decorator to be used on constructor parameters, which tells the DI framework to start dependency resolution from the parent injector. Resolution works upward through the injector hierarchy, so the local injector is not checked for a provider.

參見

選項

使用說明

在以下示例中,可以在實例化子級時解析依賴項,但在實例化類別本身時不解析。

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

      
      class Dependency {}

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

const parent = Injector.create({providers: [{provide: Dependency, deps: []}]});
const child =
    Injector.create({providers: [{provide: NeedsDependency, deps: [Dependency]}], parent});
expect(child.get(NeedsDependency).dependency instanceof Dependency).toBe(true);

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