Self

A constructor parameter decorator that tells the DI framework to retrieve a dependency only from the local injector.

查看"说明"...

参见

说明

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 = ReflectiveInjector.resolveAndCreate([Dependency, NeedsDependency]); const nd = inj.get(NeedsDependency); expect(nd.dependency instanceof Dependency).toBe(true); inj = ReflectiveInjector.resolveAndCreate([Dependency]); const child = inj.resolveAndCreateChild([NeedsDependency]); expect(() => child.get(NeedsDependency)).toThrowError();
      
      
  1. class Dependency {}
  2.  
  3. @Injectable()
  4. class NeedsDependency {
  5. constructor(@Self() public dependency: Dependency) {}
  6. }
  7.  
  8. let inj = ReflectiveInjector.resolveAndCreate([Dependency, NeedsDependency]);
  9. const nd = inj.get(NeedsDependency);
  10.  
  11. expect(nd.dependency instanceof Dependency).toBe(true);
  12.  
  13. inj = ReflectiveInjector.resolveAndCreate([Dependency]);
  14. const child = inj.resolveAndCreateChild([NeedsDependency]);
  15. expect(() => child.get(NeedsDependency)).toThrowError();

选项