尝试在父视图中动态加载子组件:
this.viewAddedSubscription = viewManager.viewAdded.subscribe((view) => {
let injector = ReflectiveInjector.resolveAndCreate([new Provider('view', { useValue: view })]);
this._componentResolver.resolveComponent(view.type).then(result => {
this.child.createComponent(result, 0, injector);
});
});
但总是得到错误:
Error: Uncaught (in promise): No provider for ViewUtils!
我做错了什么吗?如果不是,那么有人可以告诉我如何为ViewUtils添加提供程序。
答案 0 :(得分:0)
创建电流分量注入器的子进样器,而不是独立的进样器:
constructor(private injector:Injector) {}
someOtherMethod() {
let resolvedProviders = ReflectiveInjector.resolve([new Provider('view', { useValue: view })]]);
let child = ReflectiveInjector.fromResolvedProviders(resolvedProviders, this.injector);
...
}
通过这种方式,您可以传递与当前组件具有相同提供程序的注入器(Angular全局提供的所有内容)和覆盖(如果已由父级提供)或添加view
提供商。