使用.forRoot构建AOT angular5库特征模块会导致“装饰器”不支持“函数调用”

时间:2018-01-22 18:51:01

标签: angular shared-libraries static-libraries aot angular-aot

我想在一个单独的库中安装angular5功能模块。

我已经创建了一个项目并将模块声明放在其中:

@NgModule({
    imports: [],
    declarations: [],
    exports: [],
    providers: []
})
export class ApiModule {
    public static forRoot(configurationFactory: () => Configuration): ModuleWithProviders {
        return {
            ngModule: ApiModule,
            providers: [ { provide: Configuration, useFactory: configurationFactory } ]
        }
    }

    constructor( @Optional() @SkipSelf() parentModule: ApiModule) {
        if (parentModule) {
            throw new Error('ApiModule is already loaded. Import your base AppModule only.');
        }
    }
}

tsc编译后,结果是用ng build --prod编译主项目给出了:

ERROR in Error during template compile of 'AppModule'
  Function calls are not supported in decorators but 'ApiModule' was called.

1 个答案:

答案 0 :(得分:0)

问题是tsc不会发出包含angular所需装饰器的所有数据的元数据。

我使用ng-packagr https://github.com/dherges/ng-packagr(v2.0.0-rc.11)解决了这个问题