Angular 8 : Function expressions are not supported in decorators

标签 angular typescript

我在尝试在 Angular 8 中构建产品时遇到问题。错误是

ERROR in Error during template compile of 'AppModule'
  Function expressions are not supported in decorators in 'SocketLibModule'
    'SocketLibModule' references 'initializeApp'
      'initializeApp' contains the error at ../socket-lib/src/lib/socket-lib.module.ts(9,12)
        Consider changing the function expression into an exported function.

什么指向我的库

export const SOCKET_CONFIG = new InjectionToken<SocketConfig>('SOCKET_CONFIG');

export function initializeApp(socketConfig: SocketConfig) {
  return (uriConfig: any) => {
    return () => uriConfig.load(socketConfig);
  };
}

@NgModule()
export class SocketLibModule {
  public static forRoot(socketConfig: SocketConfig): ModuleWithProviders {
    return {
      ngModule: SocketLibModule,
      providers: [
        {
          provide: SOCKET_CONFIG,
          useValue: socketConfig
        },
        {
          provide: APP_INITIALIZER,
          useFactory: initializeApp(socketConfig),
          deps: [ConfigInitService, HttpClientModule],
          multi: true
        },
        HttpClientModule
      ]
    };
  }

}

错误点 (9,12) 指的是这个 return (uriConfig: any) => { 就在左括号处。

我试图将箭头函数更改为常规函数,但没有任何改变以及错误点 (9,12)

最佳答案

useFactory: initializeApp(socketConfig),

这一行就是问题所在。

应用初始化器应该是一个工厂:这意味着它应该是

useFactory: initializeApp,

否则,您将返回一个函数调用,这在 typescript 装饰器中是被禁止的(不仅在 Angular 中,还是在第 8 版中)

关于 Angular 8 : Function expressions are not supported in decorators,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56663059/

相关文章:

javascript - 自定义加载器不适用于 react-table

javascript - Angular2 npm 包 - 我如何知道要下载什么?

带接口(interface)的 Angular 6 服务

Angular 6 : Adding @Input to component doesn't work

javascript - FormArray 的 Angular .removeAt(i) 不会在 DOM 中更新 - Angular

angular - 如何在我的其他组件中排除 app.component.html 的代码

javascript - 批量获取DocumentReferences?

javascript - Angular 2 : Unexpected Token error & displaying input into a variable

javascript - 在 Angular2 中设置距离另一个元素的元素高度

angular - 使用 Observable<void> 或 Observable<any> 发出 `null` 值?