我在尝试在 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/