【发布时间】:2018-09-10 21:42:45
【问题描述】:
我正在尝试使用需要来自最终用户应用程序的配置的服务构建一个库。
但是当我构建库时,我收到了这个警告:
警告:Can't resolve all parameters for CacheService in ... 这将成为 Angular v6.x 中的错误
代码如下:
@Injectable()
export class CacheService {
private config: Config;
constructor(config: Config) {
this.config = config;
}
}
@NgModule({
})
export class MyModule {
static forRoot(config: Config): ModuleWithProviders {
return {
ngModule: MyModuleRoot,
providers: [
{ provide: CacheService, useFactory: cacheServiceFactory, deps: [config] }
]
};
}
}
export function cacheServiceFactory(config: Config): CacheService {
return new CacheService(config);
}
当我尝试在构造函数中注入 CacheService 来运行我的主应用程序时,我得到了 AppComponent_Host.ngfactory.js? [sm]:1 ERROR 错误:StaticInjectorError(AppModule)[CacheService -> [object Object]]:
但我不希望它被注入,而是使用我创建的工厂......
我的代码有什么问题?
【问题讨论】: