Angular - useFactory - 不支持错误函数调用。考虑用导出函数的引用替换函数或 lambda
Angular - useFactory - Error function calls are not supported. Consider replacing the function or lambda with a reference to an exported function
我使用Angular AOT编译器版本4.1.3
我有一个导出工厂方法的组件库,然后用于提供服务:
export function libServiceFactory(itemId: string) {
return (http: Http): LibService => {
return new LibService(http, itemId);
};
};
我用ngc
成功编译库。
然后导入编译好的库,我在网站的AppModule中使用LibServiceFactory
:
providers: [
{ provide: SERVICE_TOKEN, useFactory: libServiceFactory('foo'), deps: [Http] }
],
我用ng build
编译网站,我得到以下错误:
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 5:10 in the
original .ts file), resolving symbol libServiceFactory in C:/Projects/MyProject/code/MyWebsite/node_modules/@mylibrary/service.factory.d.ts, res
olving symbol AppModule in C:/Projects/MyProject/code/MyWebsite/src/app/app.module.ts, resolving symbol AppModule in C:/Projects/MyProject/code/MyWebsite/src/app/app.mod
ule.ts, resolving symbol AppModule in C:/Projects/MyProject/code/MyWebsite/src/app/app.module.ts
错误(position 5:10 in the original .ts file)
从工厂方法指向匿名函数:return (http: Http): LibService => {
作为附加信息,我创建和使用工厂方法的方式与 official documentation regarding injection tokens 中建议的相同 Angular。
我的错误是预期的行为吗?还是我的实现有问题?
您不能在元数据装饰器中执行代码 (@Sth)
所以你无权执行函数libServiceFactory('foo').
如果你想要一个工厂(和一些服务注入来获取 HttpClient 服务),
并同时给出一些参数b('foo' 在你的情况下)。
你必须使用以下技巧:
export const MyLibConfig = new InjectionToken<MyConfig>('MyLibConfig');
export interface MyConfig {
p: number;
msg: string;
}
export function serviceFactory(myConfig: MyConfig, http: HttpClient): MyService {
const service = new MyService(http);
service.myConfig = myConfig;
return service;
}
那么你可以使用:
providers: [
{
provide: MyService,
useFactory: serviceFactory,
deps: [MyLibConfig, HttpClient]
},
{
provide: MyLibConfig,
useValue: {p:3, msg:'foo'}
}
]
我使用Angular AOT编译器版本4.1.3
我有一个导出工厂方法的组件库,然后用于提供服务:
export function libServiceFactory(itemId: string) {
return (http: Http): LibService => {
return new LibService(http, itemId);
};
};
我用ngc
成功编译库。
然后导入编译好的库,我在网站的AppModule中使用LibServiceFactory
:
providers: [
{ provide: SERVICE_TOKEN, useFactory: libServiceFactory('foo'), deps: [Http] }
],
我用ng build
编译网站,我得到以下错误:
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 5:10 in the
original .ts file), resolving symbol libServiceFactory in C:/Projects/MyProject/code/MyWebsite/node_modules/@mylibrary/service.factory.d.ts, res
olving symbol AppModule in C:/Projects/MyProject/code/MyWebsite/src/app/app.module.ts, resolving symbol AppModule in C:/Projects/MyProject/code/MyWebsite/src/app/app.mod
ule.ts, resolving symbol AppModule in C:/Projects/MyProject/code/MyWebsite/src/app/app.module.ts
错误(position 5:10 in the original .ts file)
从工厂方法指向匿名函数:return (http: Http): LibService => {
作为附加信息,我创建和使用工厂方法的方式与 official documentation regarding injection tokens 中建议的相同 Angular。
我的错误是预期的行为吗?还是我的实现有问题?
您不能在元数据装饰器中执行代码 (@Sth) 所以你无权执行函数libServiceFactory('foo').
如果你想要一个工厂(和一些服务注入来获取 HttpClient 服务), 并同时给出一些参数b('foo' 在你的情况下)。 你必须使用以下技巧:
export const MyLibConfig = new InjectionToken<MyConfig>('MyLibConfig');
export interface MyConfig {
p: number;
msg: string;
}
export function serviceFactory(myConfig: MyConfig, http: HttpClient): MyService {
const service = new MyService(http);
service.myConfig = myConfig;
return service;
}
那么你可以使用:
providers: [
{
provide: MyService,
useFactory: serviceFactory,
deps: [MyLibConfig, HttpClient]
},
{
provide: MyLibConfig,
useValue: {p:3, msg:'foo'}
}
]