我使用 Angular 渲染器和 Angular 动态组件解决了这个问题。
Angular 动态组件加载器
父组件 HTML
<ng-template #willContainTheChildComponent></ng-template>
父组件类
@ViewChild('willContainTheChildComponent', {read: ViewContainerRef}) willContainTheChildComponent: ViewContainerRef;
constructor(private componentFactoryResolver: ComponentFactoryResolver,
private renderer: Renderer2) {
}
//The click handler
onClick: (e, row) => {
const componentFactory = this.componentFactoryResolver.resolveComponentFactory(TheComponentClass);
const component = this.willContainTheChildComponent.createComponent(componentFactory);
//Here I have access to component.instance to manipulate the class' contents
this.renderer.appendChild(row.getElement(), component.location.nativeElement);
}