【发布时间】:2021-01-11 13:28:29
【问题描述】:
我创建了一个动态组件加载器,它工作正常,但我将如何销毁其 ts.file 中的组件?
动态加载的组件:
<button class="btn btn-secondary" (click)="destroyComp()"> Destroy</button>
问题是,函数destroyComp 的实现会是什么样子?有没有我可以调用的函数让组件自行销毁而不做任何事情?
这将是工作正常的组件加载器(在应用程序组件中):
popups = [
{id:1, component: TestComponent}
]
@ViewChild('popup', {read: ViewContainerRef}) public popup: ViewContainerRef;
loadPopupComponent(i: number, event){
if(i == 0 || !this.popup) return;
const component = this.popups.find(x => x.id == i).component;
if(component){
const target = this.popup;
const widgetComponent = this.componentFactoryResolver.resolveComponentFactory(component);
let cmpRef: ComponentRef<any> = target.createComponent(widgetComponent);
}
}
和 HTML 文件
<ng-template #popup></ng-template>
【问题讨论】:
标签: angular typescript components