我正在尝试将基于运行时数据的提供程序注入(inject)到我的 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());
这尝试创建提供程序,但由于其子依赖项,它会引发错误。
我收到的错误是:
Error: No provider for HttpHandler! (GoogleRepository -> HttpHandler)
我将如何继续将子提供程序注入(inject)到已解析的提供程序中?
最佳答案
我猜你想要的是构建一个在解析时也包含 Angular2 提供程序的注入(inject)器
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
关于javascript - 通过字符串注入(inject)和解析 Angular2 提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41258817/