我写了一个带有一些服务的角度库,可以在不同的角度应用中使用它.没有–prod,所以没有AOT编译,在角度应用程序中一切正常.
使用ng-packagr以及cli以及一些不同的yeoman生成器生成库每次都会产生相同的错误.此外,我尝试了不同的ng版本(5.x.x,6.x.x& 7.x.x).但是在所有情况下,当我在应用程序的app.module中调用LoggerModule.forRoot()时,每次(使用AOT)都会出现相同的错误:
ERROR in Error during template compile of 'AppModule' Function calls are not supported in decorators but 'LoggerModule' was called.
我阅读了很多关于这个主题的文章,在tsconfig中尝试了不同的angularCompilerOptions.有什么进一步的想法吗?该模块在没有AOT的情况下工作正常(但这不是我们的选择)……
图书馆的NgModule:
@NgModule({ declarations: [],imports: [],providers: [] }) export class LoggerModule { static forRoot(): ModuleWithProviders { return { ngModule: LoggerModule,providers: [LoggerService] } } }
NgModule的应用:
@NgModule({ declarations: [ AppComponent ],imports: [ BrowserModule,AppRoutingModule,LoggerModule.forRoot() ],providers: [],bootstrap: [AppComponent],entryComponents: [AppComponent] }) export class AppModule { }
解决方法
在模块定义之外声明你的forRoot:
import { ModuleWithProviders } from ‘@angular/core’; export const forRoot: ModuleWithProviders = LoggerModule.forRoot(); @NgModule({ declarations: [ AppComponent ],imports: [ BrowserModule,forRoot ],entryComponents: [AppComponent] }) export class AppModule { }