Pat*_*ney 6 typescript angular-cli angular ngzone
我创建了一个 Angular 6 库,但是当我尝试在创建它的项目之外使用它时出现错误。这看起来像很多代码,但它主要是由 CLI 生成的样板。
我使用 Angular 6 CLI 创建了一个非常基本的库。
ng new mylib
cd mylib
ng generate library example
ng build --prod example
Run Code Online (Sandbox Code Playgroud)
然后,我可以添加 <lib-example></lib-example>
到src/app/app.component.html
、运行ng serve
并查看示例组件按预期工作。
接下来我编辑projects/example/src/lib/example.component.ts
添加一个 an*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() {
}
}
Run Code Online (Sandbox Code Playgroud)
为了获得ngIf
指令,我 import 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 { }
Run Code Online (Sandbox Code Playgroud)
重建:
ng build --prod example
Run Code Online (Sandbox Code Playgroud)
我的组件还没有做任何有用的事情,但只要我在创建它的同一个项目中使用它,它就可以工作。
让我们尝试在不同的应用程序中使用该库。我首先生成一个新应用程序,该应用程序与包含库的应用程序位于同一目录中。
ng new myapp
Run Code Online (Sandbox Code Playgroud)
然后,我添加<lib-example></lib-example>
到myapp/src/app/app.component.html
并导入库myapp/src/app/app.module.ts
,所以它看起来是这样的。
ng build --prod example
Run Code Online (Sandbox Code Playgroud)
当我浏览到应用程序时,我在控制台中收到以下错误。
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)
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
嗯,我不知道为什么。但问题在于导入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 { }
Run Code Online (Sandbox Code Playgroud)
我已经这样做了很长一段时间,并一一删除了我的库模块中的导入。这是BrowserModule
.
我认为这应该回答你的另一个问题。
您是否尝试在文件中添加以下内容tsconfig.json
:
"paths": {
"@angular/*": [
"./node_modules/@angular/*"
]
},
Run Code Online (Sandbox Code Playgroud)
./node_modules
请注意,有人建议,路径是../node_modules
后者对我不起作用。
希望这会对某人有所帮助。我花了很长时间才找到解决方案。感谢: https: //github.com/angular/angular-cli/issues/11883
归档时间: |
|
查看次数: |
6225 次 |
最近记录: |