我正在尝试将基于运行时数据的提供程序注入我的angular2组件。
到目前为止,我有这个:
let type: PropertyDescriptor = Object.getOwnPropertyDescriptor(providers, provider.name);
if (!type) return;
let injector: Injector = ReflectiveInjector.resolveAndCreate([(<any>type).get()]);
let resolvedProvider: IAnalyticRepository = injector.get((<any>type).get());
这会尝试创建提供程序,但由于它的子依赖项,它会抛出错误。
我收到的错误是:
错误:没有HttpHandler的提供程序! (GoogleRepository - &gt; HttpHandler)
我如何继续将子提供程序注入已解析的提供程序?
答案 0 :(得分:1)
我想你想要的是在解析时构建一个包含Angular2提供者的注入器
constructor(private injector:Injector) {}
...
someMethod() {
let resolvedProviders = ReflectiveInjector.resolve([Car, Engine]);
let child = ReflectiveInjector.fromResolvedProviders(
resolvedProviders,
this.injector
);
}
另见Inject service with ReflectiveInjector without specifying all classes in the dependency tree