角2:错误遇到静态解析符号值。 - 我怎样才能变成一个工厂方法呢?(Angular 2 : E

2019-09-30 11:36发布

安装角CLI后,我得到这个错误:

Error encountered resolving symbol values statically. Function calls are not supported. Consider replacing the function or lambda with a refer
ence to an exported function (position 55:19 in the original .ts file), resolving symbol AppModule in C:/TM-2013/Debt/Debt Platform/Code/main/Think.Debt.Presentation/deb-explorer-with-angular
-cli/src/app/app.module.ts    

在我的App.Module.ts它不喜欢的这部分代码:

供应商:

//   These are needed throughout the site    
AppConfigService,

{ provide: APP_INITIALIZER, useFactory: configServiceFactory, deps: [AppConfigService], multi: true },
{
  provide: HttpService,
  useFactory: (backend: XHRBackend, options: RequestOptions) => {
    return new HttpService(backend, options);
  },
  deps: [XHRBackend, RequestOptions]
}

]

编辑:

与.load

  { provide: APP_INITIALIZER, useFactory: (config: AppConfigService) => () => config.load(), deps: [AppConfigService], multi: true },
    {
      provide: HttpService,
      useFactory: (backend: XHRBackend, options: RequestOptions) => {
        return new HttpService(backend, options);
      },
      deps: [XHRBackend, RequestOptions]
    }

是否有可能变成一个工厂类,这是它不喜欢这个当前的使用情况?

Answer 1:

究竟它建议:

// named exported function
export function httpFactory(backend: XHRBackend, options: RequestOptions) {
    return new HttpService(backend, options);
}

export function configServiceFactory(config: AppConfigService) {
    return () => config.load();
}

// after goes declaration of you module with `providers`
// ... skipped ...
{ provide: APP_INITIALIZER, useFactory: configServiceFactory, deps: [AppConfigService], multi: true },
{
    provide: HttpService,
    useFactory: httpFactory,  // reference to the function
    deps: [XHRBackend, RequestOptions]
}
// ... skipped ...


文章来源: Angular 2 : Error encountered resolving symbol values statically. - How can I turn this into a factory method?