gpt4 book ai didi

angular - 在Angular 5+中创建动态组件时的ReflectiveInjector与Injector

转载 作者:搜寻专家 更新时间:2023-10-30 21:58:40 24 4
gpt4 key购买 nike

迁移到角V.5.2.9后注意到一个弃权消息:
ReflectiveInjector已被弃用:从V5-Slow引入大量代码,改用Injector.create
在Angular V5之前,我已经创建了如下动态组件:
反光镜

// Old API with ReflectiveInjector
createWithReflectiveInjector() {
// Inputs need to be in the following format to be resolved properly
const resolvedInputs = ReflectiveInjector.resolve([
{ provide: "options", useValue: this.data }
]);

// Clear the template before inserting new one
this.dynamicComponentContainer.clear();

// We create an injector out of the data we want to pass down and this components injector
const injector = ReflectiveInjector.fromResolvedProviders(
resolvedInputs,
this.dynamicComponentContainer.parentInjector
);

// We create a factory out of the component we want to create
const factory = this.resolver.resolveComponentFactory(CounterWithInject);

// We create the component using the factory and the injector
const component = factory.create(injector);

// Subscribe to Output events:
component.instance.resetCounter.subscribe(() => {
this.data.count = 0;
});

// We insert the component into the dom container
this.dynamicComponentContainer.insert(component.hostView);
}

我用injector.create()重构了更新的api:
注射器
createWithInjector() {
const inputs = [{ provide: "options", useValue: this.data }];

// Clear the template before inserting new one
this.dynamicComponentContainer2.clear();

const injector = Injector.create({
providers: inputs,
parent: this.dynamicComponentContainer2.parentInjector
});

// We create a factory out of the component we want to create
const factory = this.resolver.resolveComponentFactory(CounterWithInput);

// Destroy the previously created component
if (this.componentRef) {
this.componentRef.destroy();
}

this.componentRef = this.dynamicComponentContainer2.createComponent(
factory, 0, injector);

// We insert the component into the dom container
this.dynamicComponentContainer2.insert(this.componentRef.hostView);

this.componentRef.instance.resetCounter.subscribe(() => {
this.data.count = 0;
});
}

甚至在切换到新的注入器API之后,代码实际上也是一样的。
我的问题是:
1)也许我在文档中遗漏了一些东西,在AngularV5+中创建动态组件可以简化得多?
2)注意,在 official doc输入数据中,提供了一个组件的扩展实例,如下所示:
let componentRef = viewContainerRef.createComponent(componentFactory);
(<AdComponent>componentRef.instance).data = adItem.data;

以及如何为动态组件提供道具的首选方法:
与供应商:
const inputs = [{ provide: "options", useValue: { count: 0} }];
const injector = Injector.create({
providers: inputs,
parent: this.dynamicComponentContainer2.parentInjector
});

读取组件中的注入道具:
...
ngOnInit() {
this.options = this.injector.get("options") || {};
}

随着实例扩展:
// Define input data
this.componentRef.instance.data = { count: 0 };

并在组件中获取@input():
export class CounterWithInput implements OnInit {
@Input() data: any;
}

带方法的现场演示: link

最佳答案

我在这里演示了动态组件注入
DEMO

关于angular - 在Angular 5+中创建动态组件时的ReflectiveInjector与Injector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49819275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com