How to add another provider to the injector?

后端 未结 1 618
有刺的猬
有刺的猬 2021-02-11 06:05

A framework-agnostic way of phrasing this question is \"How to register another service with the service locator?\"

The Injector is set up to be immutable, both the inte

1条回答
  •  鱼传尺愫
    2021-02-11 06:31

    In order to add a provider to the existing injector you have to extend it by creating a new injector and pass the parent injector:

    class Comp {
       constructor(existing: Injector) {
          const newInjector = ReflectiveInjector.resolveAndCreate(providers, existing)
       }
    }
    

    To get more details read Difference between Reflective Injector and Injector in Angular.

    How does Angular do this itself when it is loading new modules after bootstrapping, for example via the router?

    It uses a bit different mechanism. When a new module instance is created it's passed the parent injector:

    class NgModuleRef_ implements NgModuleData, InternalNgModuleRef {    
      constructor(..., public _parent: Injector) {
        initNgModule(this);
      }
    

    And then when you request a token it uses this parent injector to resolve dependency if it's not found on the existing injector:

    NgModuleRef_.get(token, notFoundValue = Injector.THROW_IF_NOT_FOUND) {
         return resolveNgModuleDep(...);
    }
    
    export function resolveNgModuleDep(...) {
      ...
      if (found) return instance;
      return data._parent.get(depDef.token, notFoundValue);  <----------------
    }
    

    0 讨论(0)
提交回复
热议问题