【发布时间】:2021-03-28 23:16:38
【问题描述】:
该代码有什么问题:
@Directive({
selector: '[appDir]'
})
export class DirDirective {
constructor(
private vcr: ViewContainerRef,
private compFactoryResolver: ComponentFactoryResolver
) {}
ngAfterViewInit() {
const compAFactory = this.compFactoryResolver.resolveComponentFactory(
CompAComponent
);
const compBFactory = this.compFactoryResolver.resolveComponentFactory(
CompBComponent
);
const compARef = this.vcr.createComponent(compAFactory);
this.vcr.createComponent(compBFactory, 0, undefined, [
[compARef.location.nativeElement]
]);
}
}
仅在启用 IVY 时失败
https://stackblitz.com/edit/angular-h3wqc4?file=src%2Fapp%2Fdir.directive.ts
【问题讨论】:
-
最终目标是什么?
-
我想使用指令而不是组件。如代码所示,该指令应(动态)创建 2 个组件,并使用 projectableNodes 将其中一个组件插入另一个组件
标签: angular angular-directive ivy angular-dynamic-components