Angular: Metadata collected contains an error that will be reported at runtime: Lambda not supported

牧云@^-^@ 提交于 2020-06-27 14:50:08

问题


In my Angular app, I'm trying to use a factory provider in my module:

export function getMyFactory(): () => Window {
  return () => window;
}

@NgModule({
  providers: [
    { provide: WindowRef, useFactory: getMyFactory() },
  ],
})
export class MyModule {}

but this is failing with:

Error encountered in metadata generated for exported symbol 'MyModule':

Metadata collected contains an error that will be reported at runtime: Lambda not supported


回答1:


I've found an easy solution reported on a thread from GitHub: Arrow lambda not supported in static function posted by haochi

The solution is basically:

assigning the result to a variable, then return the variable


So in my case, I've resolved by replacing:

export function getMyFactory(): () => Window {
  return () => window;
}

with:

export function getMyFactory(): () => Window {
  const res = () => window;
  return res;
}



回答2:


I had the same issue trying to return a promise as a function, and replaced this:

export function myFunc(): Function {
    const result = () => ...
    return result;
}

With:

export function myFunc(){
    const result = () => ...
    return result;
}


来源:https://stackoverflow.com/questions/57594723/angular-metadata-collected-contains-an-error-that-will-be-reported-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!