angular2 AoT错误不支持函数调用

angular2 AoT error Function calls are not supported

本文关键字:函数调用 不支持 错误 AoT angular2      更新时间:2023-09-26

当我使用angular2 AoT时,我得到一个错误:

 Function calls are not supported. Consider replacing the function or lambda with a reference to an exported function (position 54:17 in the original .ts file), resolving symbol COMPILER_PROVIDERS in

和在我的指令模块,我有这样的代码:

import { COMPILER_PROVIDERS } from '@angular/compiler';
@NgModule({
/*imports ...*/
providers: [
    COMPILER_PROVIDERS,
]
})

我明白我应该把COMPILER_PROVIDERS改成一个导出的函数,但是当我检查@angular/compiler的源代码时,我发现这个:

export declare const COMPILER_PROVIDERS: Array<any | Type<any> | {
    [k: string]: any;
} | any[]>;
export declare class RuntimeCompilerFactory implements CompilerFactory {
    private _defaultOptions;
    constructor(defaultOptions: CompilerOptions[]);
    createCompiler(options?: CompilerOptions[]): Compiler;
}

我不知道COMPILER_PROVIDERS是如何工作的,我不知道如何将它转移到我的模块中的导出函数。

解决方案是不再使用COMPILER_PROVIDERS。同样,您也不需要在您的提供者列表中包含JitCompiler

应该使用"@angular/compiler"中的JitCompilerFactory。它是不可注入的,所以只需自己创建一个新的实例,如下所示:

private compiler: Compiler = new JitCompilerFactory([{useDebug: false, useJit: true}]).createCompiler();

其余部分和以前一样,例如Radim Kohler在这里的出色回答。