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

SkipSelf

A 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) { this.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();
      
      
  1. class Dependency {}
  2.  
  3. @Injectable()
  4. class NeedsDependency {
  5. constructor(@SkipSelf() public dependency: Dependency) { this.dependency = dependency; }
  6. }
  7.  
  8. const parent = Injector.create({providers: [{provide: Dependency, deps: []}]});
  9. const child =
  10. Injector.create({providers: [{provide: NeedsDependency, deps: [Dependency]}], parent});
  11. expect(child.get(NeedsDependency).dependency instanceof Dependency).toBe(true);
  12.  
  13. const inj = Injector.create(
  14. {providers: [{provide: NeedsDependency, deps: [[new Self(), Dependency]]}]});
  15. expect(() => inj.get(NeedsDependency)).toThrowError();

Learn more in the Dependency Injection guide.