不支持函数调用.考虑使用对导出函数的引用替换函数或lambda [英] Function calls are not supported. Consider replacing the function or lambda with a reference to an exported function

查看:45
本文介绍了不支持函数调用.考虑使用对导出函数的引用替换函数或lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用APP_INITIALIZER,并通过必要的导入将其设置在app.module.ts中,如下所示:

I'm using APP_INITIALIZER in my app and I have it setup in app.module.ts as follows with the necessary imports:

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

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

I get the following error when I build fresh (ng build -watch), subsequent builds work fine.

ERROR in Error遇到静态解析符号值的情况. 不支持函数调用.考虑更换功能或 lambda并引用了导出的函数(在 .ts原始文件),在C:/.../app.module.ts

ERROR in 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 24:46 in the original .ts file), resolving symbol AppModule in C:/.../app.module.ts

我尝试将() => context.load()移到与该文件相同的导出函数中:

I have tried moving () => context.load() into an exported function in the same file as so:

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

...然后更改了@NgModule的providers部分:

...then altered the providers section of @NgModule:

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

最初,构建仍然会失败,并出现相同的错误.随后的构建工作正常.

The build still fails initially as above with the same error. Subsequent builds work fine.

如何解决此初始构建错误?

How do I solve this initial build error?

推荐答案

将内联闭包移动到函数:

Move the inline closure to a function:

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

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

另请参见 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆