填写这份《一分钟调查》,帮我们(开发组)做得更好!去填写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();