【问题标题】:Function calls are not supported. Consider replacing the function or lambda with a reference to an exported function不支持函数调用。考虑将函数或 lambda 替换为对导出函数的引用
【发布时间】:2017-03-24 11:56:27
【问题描述】:

我在我的应用程序中使用 APP_INITIALIZER,并在 app.module.ts 中进行了如下设置,并带有必要的导入:

@NgModule({
  ...
  providers: [ ..., ContextService, { provide: APP_INITIALIZER, useFactory: (context: ContextService) => () => context.load(), deps: [ContextService], multi: true } ],
  ...
})

当我重新构建 (ng build -watch) 时出现以下错误,后续构建工作正常

错误中的错误遇到静态解析符号值。 不支持函数调用。考虑更换功能或 lambda 引用导出的函数(位置 24:46 在 原始 .ts 文件),解析 C:/.../app.module.ts 中的符号 AppModule

我已经尝试将() => context.load() 移动到同一个文件中的导出函数中:

export function loadContext(context: ContextService) {
    return () => context.load();
}

...然后修改了@NgModule 的提供者部分:

@NgModule({
      ...
      providers: [ ..., ContextService, { provide: APP_INITIALIZER, useFactory: (context: ContextService) => loadContext(context), deps: [ContextService], multi: true } ],
      ...
    })

最初的构建仍然像上面一样失败,并出现同样的错误。后续构建工作正常。

如何解决这个初始构建错误?

【问题讨论】:

标签: angular typescript lambda


【解决方案1】:

将内联闭包移动到函数中:

function loadContext(context: ContextService) {
  return () => context.load();
}

@NgModule({
  ...
  providers: [ ..., ContextService, { provide: APP_INITIALIZER, useFactory: loadContext, deps: [ContextService], multi: true } ],
  ...
})

另见How to pass parameters rendered from backend to angular2 bootstrap method

【讨论】:

  • 很高兴听到。感谢您的反馈:)
猜你喜欢
  • 1970-01-01
  • 2017-12-01
  • 2017-06-10
  • 2017-08-27
  • 1970-01-01
  • 2020-09-25
  • 2018-03-28
  • 1970-01-01
  • 2017-04-26
相关资源
最近更新 更多