简体   繁体   English

ngZone未定义-仅限Ajectable AOT模式内的注入失败

[英]ngZone is undefined - Injection failure inside Injectable AOT mode only

I am experiencing a weird issue. 我遇到一个奇怪的问题。

I have a NotificationService that calls Bootstrap notify to show some growl style messages. 我有一个NotificationService ,它调用Bootstrap notify来显示一些咆哮样式的消息。 I am trying to inject ngZone in the constructor. 我试图将ngZone注入构造函数中。 This works in JIT mode but fails in AOT mode with a "cannot call runOutSideAngular() of undefined". 这在JIT模式下有效,但在AOT模式下失败,并且“无法调用未定义的runOutSideAngular() ”。 Further debugging reveals that the constructor injection is failing in AOT mode but is injected fine in Dev (JIT) mode. 进一步的调试显示,构造函数注入在AOT模式下失败,但在Dev(JIT)模式下注入良好。

However, I am also injecting ngZone in my app.component.ts and it's injected here just fine in both AOT and JIT. 但是,我也在我的app.component.ts中注入了ngZone ,并且在AOT和JIT中都注入了它。

btw, the Service is loaded as a provider in the root module. 顺便说一句,该服务将作为提供程序加载到根模块中。 I am not able to figure out why the Injection is failing. 我无法弄清楚注入失败的原因。 I've also tried explict injection using @Inject(NgZone) and also trying to defer the injection using a forwardRef to no avail. 我还尝试使用@Inject(NgZone)进行显式注入,并且还尝试使用forwardRef推迟注入,但无济于事。

In Dev/JIT: 在Dev / JIT中:

在Dev / JIT模式下 In Prod/AOT: 在Prod / AOT中:

在AOT模式下

Here is my NotificationService: 这是我的NotificationService:

  export namespace Notification {
  @Injectable()
  export class NotificationService {
    notifier: any;

    constructor(private ngZone: NgZone) {
      console.log('message from NotificationService constructor ngZone is ', this.ngZone);
    }

What am I missing here? 我在这里想念什么?

For anyone else who wants to know the issue that caused this - it was due to Namespaces. 对于任何其他想知道导致此问题的问题的人-这是由于命名空间引起的。 For whatever reason injection does not work if the class is within a Namespace. 无论什么原因,如果类在命名空间中,注入将不起作用。 Not sure if this is an issue with Angular itself. 不确定这是否与Angular本身有关。 I had to remove the namespace after which injection worked as normal - not ideal as I wanted to use Namespaces but I can live with it. 我必须删除命名空间,之后注入才能正常进行-不理想,因为我想使用命名空间,但我可以使用它。

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

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