作为角色的初学者,我在尝试通过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
时,应用程序可以正常运行。
答案 0 :(得分:5)
试试这个:
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
答案 1 :(得分:1)
之前我已经看过这个,并且不确定是什么导致了这个错误(例如,为什么它在某些情况下有效,而其他情况却没有),但您可以通过执行错误说明来解决它: "考虑用引用替换函数或lambda 导出功能" 。
export function httpFactory(backend: XHRBackend, options: RequestOptions) {
return new HttpService(backend, options);
}
...
providers: [
{
provide: HttpService,
useFactory: httpFactory,
deps: [XHRBackend, RequestOptions]
}
]
答案 2 :(得分:1)
使用显式函数代替内联函数:
function httpFactory(backend: XHRBackend, options: RequestOptions) {
return new HttpService(backend, options);
}
...
providers: [{ provide: HttpService, useFactory: httpFactory,
deps: [XHRBackend, RequestOptions]}
]
...