Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何向注入器添加另一个提供程序?_Angular_Dependency Injection_Angular Di - Fatal编程技术网

Angular 如何向注入器添加另一个提供程序?

Angular 如何向注入器添加另一个提供程序?,angular,dependency-injection,angular-di,Angular,Dependency Injection,Angular Di,一种与框架无关的措辞是“如何向服务定位器注册另一个服务?” 注入器被设置为不可变的,包括接口和实现 interface Injector { abstract get(token: any, notFoundValue?: any): any; } 接口 实施 如何添加另一个提供程序(动态地,而不是通过模块) 当Angular在引导后(例如通过路由器)加载新模块时,它自己是如何做到这一点的?为了向现有注入器添加提供程序,您必须通过创建新注入器并传递父注入器来扩展它: class Com

一种与框架无关的措辞是“如何向服务定位器注册另一个服务?”

注入器被设置为不可变的,包括接口和实现

interface Injector {
    abstract get(token: any, notFoundValue?: any): any;
}
接口 实施

如何添加另一个提供程序(动态地,而不是通过模块)


当Angular在引导后(例如通过路由器)加载新模块时,它自己是如何做到这一点的?

为了向现有注入器添加提供程序,您必须通过创建新注入器并传递父注入器来扩展它:

class Comp {
   constructor(existing: Injector) {
      const newInjector = ReflectiveInjector.resolveAndCreate(providers, existing)
   }
}
class NgModuleRef_ implements NgModuleData, InternalNgModuleRef<any> {    
  constructor(..., public _parent: Injector) {
    initNgModule(this);
  }
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);  <----------------
}
要获得更多详细信息,请阅读

Angular在之后加载新模块时如何做到这一点 引导,例如通过路由器

它使用了一点不同的机制。创建新模块实例时,会将其传递给父注入器:

class Comp {
   constructor(existing: Injector) {
      const newInjector = ReflectiveInjector.resolveAndCreate(providers, existing)
   }
}
class NgModuleRef_ implements NgModuleData, InternalNgModuleRef<any> {    
  constructor(..., public _parent: Injector) {
    initNgModule(this);
  }
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);  <----------------
}
NgModuleRef类实现NgModuleData,InternalNgModuleRef{
构造函数(…,公共_父项:注入器){
初始化模块(this);
}
然后,当您请求令牌时,如果在现有注入器上找不到,它将使用此父注入器来解决依赖关系:

class Comp {
   constructor(existing: Injector) {
      const newInjector = ReflectiveInjector.resolveAndCreate(providers, existing)
   }
}
class NgModuleRef_ implements NgModuleData, InternalNgModuleRef<any> {    
  constructor(..., public _parent: Injector) {
    initNgModule(this);
  }
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);  <----------------
}
NgModuleRef.get(令牌,notFoundValue=Injector.THROW,如果找不到){
返回resolveNgModuleDep(…);
}
导出函数resolveNgModuleDep(…){
...
如果(找到)返回实例;

返回数据。_parent.get(depDef.token,notFoundValue);很有趣。这似乎有两个后果:(1)在引导模块后,无法向模块添加任何可注入内容(2)无法请求父模块中的子模块的可注入内容。因为父注入器不知道它的存在。