简体   繁体   English

手动TypeScript Angular依赖项注入在ChangeDetectorRef上失败

[英]manual TypeScript Angular dependency injection failing on ChangeDetectorRef

In my Angular application, I had implemented the method of manual dependency injection for TypeScript child classes described in this blog . 在我的Angular应用程序中,我已经实现了本博客中描述的TypeScript子类的手动依赖项注入方法。 The goal was to eliminate having to inject providers into child classes just to pass them into the parent class. 目的是消除将提供程序注入子类的麻烦,而不必将它们传递给父类。 It worked fine at the time. 当时效果很好。

Sometime later, when I went to use it in a second Angular module, it failed. 一段时间后,当我在另一个Angular模块中使用它时,它失败了。 Searching uncovered that the previous method might (should?) have broken in Angular 7 (see this question ), but the first use case continued to function. 搜索发现,以前的方法可能(应该?)在Angular 7中已损坏(请参阅此问题 ),但是第一个用例继续起作用。

I narrowed it down to a specific injection that fails: ChangeDetectorRef (CDR). 我将其范围缩小到失败的特定注入: ChangeDetectorRef (CDR)。 If I wrap each attempted, manual injection in a try/catch the ones after CDR are injected just as if specified in the constructor. 如果我包装了所有尝试的内容,则手动注入尝试/捕获CDR注入之后的尝试,就像在构造函数中指定的那样。

Is there something different or special about CDR? CDR有什么不同或特殊之处吗? I can't provide it, since it comes from Angular. 我无法提供它,因为它来自Angular。 Is there something about it having to be tied to the child class? 是否有必要将其与孩子班联系起来?

Ultimately, I want to know if there's some way to make it work with the manual injection system, or if it has to remain as an automatic injection from every child class. 最终,我想知道是否有某种方法可以使其与手动注射系统一起使用,或者是否必须保留每个孩子班级的自动注射功能。

Addendum: 附录:

The specific error is 具体错误是

StaticInjectorError(AppModule)[ChildComponent -> ChangeDetectorRef]:
  StaticInjectorError(Platform: core)[ChildComponent -> ChangeDetectorRef]:
    NullInjectorError: No provider for ChangeDetectorRef!

For reasons that aren't documented, ChangeDetectorRef (as well as ElementRef , ViewContainerRef and TemplateRef ) can't be manually injected in this way. 对于未记录的,原因ChangeDetectorRef (以及ElementRefViewContainerRefTemplateRef )不能以这种方式被手动注射。

See https://github.com/angular/angular/issues/14656 参见https://github.com/angular/angular/issues/14656

ChangeDetectorRef has to remain injected in the child classes and passed to super() . ChangeDetectorRef必须保留注入子类中并传递给super()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM