[英]How to get rid of Function calls are not supported in decorators in Angular aot compiling?
I am testing a Highcharts Angular2x Wrapper .我正在测试Highcharts Angular2x Wrapper 。 At first, I had no problem using Angular CLI (1.6.1) "ng serve" and profiling performance with Chrome.
起初,我在使用 Angular CLI (1.6.1)“ng serve”和 Chrome 性能分析方面没有问题。 Then, i tried to use ahead-of-time compiling to see how that affects the performance.
然后,我尝试使用提前编译来查看这如何影响性能。
So, using:所以,使用:
ng serve --aot
I get the following error:我收到以下错误:
ERROR in Error during template compile of 'AppModule'
Function calls are not supported in decorators but 'ChartModule' was called.
Now, i know that aot generates factory code for modules and somehow "transformes" templates to VanillaJS, things get a bit tricky here and i couldn't understand how ngc is going to generate factory code for a module that requires an external library.现在,我知道 aot 为模块生成工厂代码并以某种方式将模板“转换”为 VanillaJS,这里的事情有点棘手,我无法理解 ngc 将如何为需要外部库的模块生成工厂代码。
I got this App.Module.ts:我得到了这个 App.Module.ts:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { ChartModule } from 'angular2-highcharts';
import { AppComponent } from './app.component';
declare var require: any;
export function getHighchartsModule() {
return require('highcharts');
}
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
ChartModule.forRoot(getHighchartsModule) // This causes the error
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
My Package.json dependencies :我的 Package.json 依赖项:
"dependencies": {
"@angular/animations": "^5.0.0",
"@angular/common": "^5.0.0",
"@angular/compiler": "^5.0.0",
"@angular/core": "^5.0.0",
"@angular/forms": "^5.0.0",
"@angular/http": "^5.0.0",
"@angular/platform-browser": "^5.0.0",
"@angular/platform-browser-dynamic": "^5.0.0",
"@angular/router": "^5.0.0",
"angular2-highcharts": "^0.5.5",
"core-js": "^2.4.1",
"rxjs": "^5.5.2",
"zone.js": "^0.8.14"
}
My questions are : Is there anything i can do here to avoid the mentioned compiling error ?我的问题是:我可以在这里做些什么来避免提到的编译错误? Can anyone explain why does this happen ?
谁能解释为什么会发生这种情况? (optional)
(可选)
This is a problem with angular in general.这通常是 angular 的问题。 Angular compiler wants the
forRoot
code to be completely static. Angular 编译器希望
forRoot
代码完全静态。
As an example, in the following code even the assignment of the static variable will cause this error:例如,在以下代码中,即使是静态变量的赋值也会导致此错误:
static forRoot(config: MyConfig): ModuleWithProviders {
MyConfigService.config = config;// This line with cause an error
return {
ngModule: HttpTrackerLibModule,
};
}
If you are not library creator there is nothing much you can do but try library-specific solutions like the one above.如果您不是库的创建者,除了尝试上述特定于库的解决方案外,您无能为力。 But if you are a library creator you can try a static approach as follows:
但是,如果您是库创建者,则可以尝试以下静态方法:
Create an injection token:创建注入令牌:
export const USER_OPTIONS = new InjectionToken<MyConfig>('USER_OPTIONS');
Provide the token in your library Module在您的库模块中提供令牌
static forRoot(config: MyConfig): ModuleWithProviders { return { ngModule: HttpTrackerLibModule, providers: [{ provide: USER_OPTIONS, useValue: config }], }; }
export class ConfigService { constructor(@Inject(USER_OPTIONS) private _config: MyConfig) { } }
Mentioning the Github issue here .在这里提到 Github 问题。 The following solution worked for me.
以下解决方案对我有用。
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
// Angular Highcharts Imports
import {HighchartsStatic} from 'angular2-highcharts/dist/HighchartsService';
import { ChartModule } from 'angular2-highcharts';
// Factory Function
export function highchartsFactory() {
var hc = require('highcharts');
return hc;
}
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
ChartModule // Import Module Here
],
providers: [ // Provide Service Here
{
provide: HighchartsStatic,
useFactory: highchartsFactory
},
],
bootstrap: [AppComponent]
})
export class AppModule { }
I've encountered this same issue.我遇到过同样的问题。 Try removing the
getHighchartsModule
export from your App.Module.ts
and put the exported function in its own file.尝试删除
getHighchartsModule
从出口App.Module.ts
并把导出的函数在它自己的文件。 Then import it into App.Module.ts
.然后将其导入
App.Module.ts
。
I have not figured out why this happens yet.我还没有弄清楚为什么会发生这种情况。
I'd like to propose an additional answer if it helps folks get past the error.如果它可以帮助人们克服错误,我想提出一个额外的答案。 I had two nearly identical applications on Angular 11 sharing the same Library module that has a
static forRoot
method declaration for custom injection configuration.我在Angular 11上有两个几乎相同的应用程序,它们共享相同的库模块,该模块具有用于自定义注入配置的
static forRoot
方法声明。
@NgModule()
export class TheModule {
static forRoot(...) {
...
}
}
App A worked as expected, App B threw the error in question. App A 按预期工作,App B 抛出了有问题的错误。 Of course my two apps were different in nature, so this narrows it down to
infrastructure
or configuration
differences.当然,我的两个应用程序本质上是不同的,所以这将其缩小到
infrastructure
或configuration
差异。
View the shared module's compiled
code eg the-module.module.d.ts
to see if there are differences there:查看共享模块的
compiled
代码,例如the-module.module.d.ts
以查看是否存在差异:
from the scope of the working application:从工作应用的范围来看:
// the-module.module.d.ts
import * ...
...
export declare class TheModule {
static ɵmod: ɵngcc0.ɵɵNgModuleDefWithMeta<TheModule, [typeof ɵngcc4.AComponent, ...]
static ɵinj: ɵngcc0.ɵɵInjectorDef<TheModule>;
}
Note the emod
(Module) and einj
(Injection) definitions.请注意
emod
(模块)和einj
(注入)定义。
from the scope of broken app从损坏的应用程序的范围
It is missing these definitions :它缺少这些定义:
// the-module.module.d.ts
export declare class TheModule {
}
Ok so why is the module empty for the broken app!?好的,那么为什么损坏的应用程序的模块是空的!? It must be something with compilation!
它必须是编译的东西! Looking at the root
tsconfig.json
查看根
tsconfig.json
For the broken app:对于损坏的应用程序:
The following angularCompilerOptions
setting was enabled:启用了以下
angularCompilerOptions
设置:
// woops, remove this to use the newer compiler for angular versions > 9.0
"angularCompilerOptions": {
"enableIvy": false
}
This was flack left over from previous days.这是前几天留下的疤痕。 Upon removing this, the error went away , and it enabled the compiler to interact with the shared library module and its types were populated.
删除它后,错误消失了,它使编译器能够与共享库模块交互并填充其类型。
When compiling AOT take a look at the compiler options documentation: https://angular.io/guide/angular-compiler-options to see the details.编译 AOT 时,请查看编译器选项文档: https : //angular.io/guide/angular-compiler-options以查看详细信息。 The error message that @AngularJs prints, treating it as a
decorator
is a "side effect", and unrelated. @AngularJs 打印的错误消息,将其视为
decorator
是一种“副作用”,并且不相关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.