使用Injector类代替ReflectiveInjector类

问题描述 投票:0回答:1

我试图以角度动态加载数据。在我的方法中,我使用下面的代码来加载数据 -

 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()方法。

Link 1 Link 2

angularjs angular angularjs-scope
1个回答
0
投票

我找到了答案。以下是我使用过的代码 -

 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);

人们可以在这里阅读更多1 2

© www.soinside.com 2019 - 2024. All rights reserved.