NullInjectorError:NgZone没有提供者! (Angular 6库)

时间:2018-07-30 20:40:01

标签: angular typescript angular-cli ngzone

摘要

我创建了一个Angular 6库,但是当我尝试在创建它的项目之外使用它时出现错误。这看起来像很多代码,但主要是CLI生成的样板文件。

最小工作测试用例

我使用Angular 6 CLI创建了一个非常基本的库。

ng new mylib 
cd mylib
ng generate library example
ng build --prod example

然后我可以将<lib-example></lib-example>添加到src/app/app.component.html,运行ng serve,然后查看示例组件是否按预期工作。

接下来,我编辑projects/example/src/lib/example.component.ts,将*ng-if="true"添加到<p>标签中。

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'lib-example',
  template: `
    <p *ng-if="true"> <!-- this line was changed -->
      example works!
    </p>
  `,
  styles: []
})
export class ExampleComponent implements OnInit {

  constructor() { }

  ngOnInit() {
  }

}

要获取ngIf指令,请导入BrowserModule,因此我的projects/example/src/lib/example.module.ts如下所示:

import { NgModule } from '@angular/core';
import { ExampleComponent } from './example.component';
import { BrowserModule } from '@angular/platform-browser'; // added

@NgModule({
  imports: [
    BrowserModule // added
  ],
  declarations: [ExampleComponent],
  exports: [ExampleComponent]
})
export class ExampleModule { }

重建:

ng build --prod example

我的组件还没有做任何有用的事情,但是只要我在用于创建它的同一项目中使用它,它就可以工作。

最小无效测试用例

让我们尝试在其他应用中使用该库。我首先生成一个新应用程序,该应用程序与包含该库的应用程序位于同一目录中。

ng new myapp 

然后我将<lib-example></lib-example>添加到myapp/src/app/app.component.html并将库导入到myapp/src/app/app.module.ts中,使它看起来像这样。

import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";

import { AppComponent } from "./app.component";
import { ExampleModule } from "../../../mylib/dist/example"; // added

@NgModule({
  declarations: [AppComponent],
  imports: [
     BrowserModule,
     ExampleModule // added
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}

错误消息

浏览到应用程序时,在控制台中出现以下错误。

Error: StaticInjectorError(AppModule)[ApplicationRef -> NgZone]: 
  StaticInjectorError(Platform: core)[ApplicationRef -> NgZone]: 
    NullInjectorError: No provider for NgZone!
    at NullInjector.push../node_modules/@angular/core/fesm5/core.js.NullInjector.get (core.js:1060)
    at resolveToken (core.js:1298)
    at tryResolveToken (core.js:1242)
    at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:1139)
    at resolveToken (core.js:1298)
    at tryResolveToken (core.js:1242)
    at StaticInjector.push../node_modules/@angular/core/fesm5/core.js.StaticInjector.get (core.js:1139)
    at resolveNgModuleDep (core.js:8377)
    at _createClass (core.js:8430)
    at _createProviderInstance (core.js:8394)

我在做什么错了?

2 个答案:

答案 0 :(得分:2)

您是否尝试过在tsconfig.json文件中添加以下内容:

"paths": {
      "@angular/*": [
        "./node_modules/@angular/*"
      ]
    },

请注意,路径为./node_modules,有人建议使用../node_modules,后者对我不起作用。

希望这会对某人有所帮助。我花了很长时间才找到解决方案。感谢:https://github.com/angular/angular-cli/issues/11883

答案 1 :(得分:1)

嗯,我不知道为什么。但是问题在于导入BrowserModule。不要将其导入到您的库模块中,它应该可以工作。

import { NgModule } from '@angular/core';
import { ExampleComponent } from './example.component';
//import { BrowserModule } from '@angular/platform-browser'; // added

@NgModule({
  imports: [
   // BrowserModule // removed
  ],
  declarations: [ExampleComponent],
  exports: [ExampleComponent]
})
export class ExampleModule { }

我已经很长时间了,并且逐一删除了我的库模块中的导入内容。是BrowserModule

我认为这应该回答您其他的question