How to add another provider to the injector?

爱⌒轻易说出口 提交于 2019-12-04 14:33:31

问题


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 interface and the implementation.

interface Injector {
    abstract get(token: any, notFoundValue?: any): any;
}

interface https://github.com/angular/angular/blob/master/packages/core/src/di/injector.ts implementation https://github.com/angular/angular/blob/master/packages/core/src/di/reflective_injector.ts

How do you add another provider (dynamically, not via a module)?

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


回答1:


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<any> {    
  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);  <----------------
}


来源:https://stackoverflow.com/questions/45479701/how-to-add-another-provider-to-the-injector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!