NullInjectorError:NgZone没有提供者! (Angular 6库) [英] NullInjectorError: No provider for NgZone! (Angular 6 library)
问题描述
我创建了一个Angular 6库,但是尝试在创建它的项目之外使用它时收到错误消息.这看起来像很多代码,但主要是CLI生成的样板文件.
I created an Angular 6 library, but I get an error when I try to use it outside of the project in which it was created. This looks like a lot of code, but it's mostly boilerplate generated by the CLI.
我使用Angular 6 CLI创建了一个非常基本的库.
I created a very basic library with the 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
,然后查看示例组件是否按预期工作.
I can then add <lib-example></lib-example>
to src/app/app.component.html
, run ng serve
, and see the example component works as expected.
接下来,我编辑projects/example/src/lib/example.component.ts
,将*ng-if="true"
添加到<p>
标签.
Next I edit projects/example/src/lib/example.component.ts
to add an an *ng-if="true"
to the <p>
tag.
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
如下所示:
To get the ngIf
directive, I import BrowserModule
, so my projects/example/src/lib/example.module.ts
looks like this:
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
我的组件还没有做任何有用的事情,但是只要我在用于创建它的同一个项目中使用它,它就可以工作.
My component doesn't do anything useful yet, but it works as long as I use it within the same project I used to create it.
让我们尝试在其他应用中使用该库.我首先生成一个新应用程序,该应用程序与包含该库的应用程序位于同一目录中.
Let's try using the library in a different app. I start by generating a new app that lives in the same directory as the app that contains the library.
ng new myapp
然后我将<lib-example></lib-example>
添加到myapp/src/app/app.component.html
并将库导入到myapp/src/app/app.module.ts
中,使其看起来像这样.
Then I add <lib-example></lib-example>
to myapp/src/app/app.component.html
and import the library in myapp/src/app/app.module.ts
, so that it looks like this.
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 Message
When I browse to the app, I get the following error in the console.
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)
我在做什么错了?
推荐答案
好吧,我不知道为什么.但是问题出在导入BrowserModule
上.不要将其导入到您的库模块中,它应该可以工作.
Well, I don't know why. But the problem is with importing the BrowserModule
. Do not import it into your library module and it should work.
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
.
I have been in this for a very long time and removed the imports in my library module one by one. It was the BrowserModule
.
我认为这应该回答您其他的问题.
I think this should answer your other question.
这篇关于NullInjectorError:NgZone没有提供者! (Angular 6库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!