我试图以角度动态加载数据。在我的方法中,我使用下面的代码来加载数据 -
const inputProviders = Object.keys(data.inputs).map(inputName => {
return { provide: inputName, useValue: data.inputs[inputName] };
});
const resolvedInputs = ReflectiveInjector.resolve(inputProviders);
const injector = ReflectiveInjector.fromResolvedProviders(
resolvedInputs,
this.cardComponent.parentInjector
);
const componentType = this.content.resolve(data.component);
const factory = this.resolver.resolveComponentFactory(componentType);
const component = factory.create(injector);
this.cardComponent.insert(component.hostView);
Lint指出ReflectiveInjector将被折旧。我应该如何使用Injector以使我当前的代码能够正常工作。我尝试过以下页面中提到的方法,但它没有说明resolve()和fromResolvedProviders()方法。
我找到了答案。以下是我使用过的代码 -
const inputProviders = Object.keys(data.inputs).map(inputName => {
return { provide: inputName, useValue: data.inputs[inputName] };
});
const injector = Injector.create(
{ providers: inputProviders,
parent : this.cardComponent.parentInjector
});
const componentType = this.content.resolve(data.component);
const factory = this.resolver.resolveComponentFactory(componentType);
const component = factory.create(injector);
this.cardComponent.insert(component.hostView);