angular2构建问题useFactory与功能

kaz*_*ipi 3 dependency-injection angular

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

在遵循了本教程之后

我的appModule看起来像:

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

构建它时,我得到:

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)
Run Code Online (Sandbox Code Playgroud)

当我使用ng serve该应用程序时,没有任何问题。

小智 6

尝试这个:

export function httpServiceFactory(backend: XHRBackend, options: RequestOptions) {
  return new HttpService(backend, options);
}  
  
providers: [
  { 
    provide: HttpService, 
    useFactory: httpServiceFactory,
    deps: [XHRBackend, RequestOptions]
  }
]
Run Code Online (Sandbox Code Playgroud)

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

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