gpt4 book ai didi

angular2 构建问题 useFactory 与功能

转载 作者:太空狗 更新时间:2023-10-29 18:07:12 26 4
gpt4 key购买 nike

作为 Angular 的初学者,我在尝试通过 angular-cli 构建我的应用程序时遇到了一个问题。

关注此tutorial之后:

我的 appModule 看起来像:

...
providers: [
{ provide: HttpService, useFactory: (backend: XHRBackend, options: RequestOptions) => {
return new HttpService(backend, options); },
deps: [XHRBackend, RequestOptions]}
]
...

构建它时我得到:

Error: Error encountered resolving symbol values statically. Function calls are
not supported. Consider replacing the function or lambda with a reference to an
exported function (position 63:39 in the original .ts file), resolving symbol Ap
pModule in ../angular/app/src/app/app.module.ts
at positionalError (..\angular\app\node_modules\@angular\compiler-c
li\src\static_reflector.js:595:18)

当我使用 ng serve 时,应用程序可以正常运行。

最佳答案

试试这个:

export function httpServiceFactory(backend: XHRBackend, options: RequestOptions) {
return new HttpService(backend, options);
}

providers: [
{
provide: HttpService,
useFactory: httpServiceFactory,
deps: [XHRBackend, RequestOptions]
}
]

当您构建它时,它会尝试进行 AoT 编译,但在遇到 lambda 函数时会失败,因此您需要导出它。当使用 ng-serve 时,它​​使用 JiT 编译。

参见:https://github.com/angular/angular/issues/11262#issuecomment-244266848

关于angular2 构建问题 useFactory 与功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42051200/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com