angular - 如何在 Angular 的 ts 文件中销毁动态加载的组件?
问题描述
我创建了一个动态组件加载器,它工作正常,但我将如何销毁其 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>
解决方案
推荐阅读
- css - 仅使用 CSS 隐藏元素但不嵌套子元素
- javascript - 我应该在哪里放置第三个 js 函数以及如何在 .vue 文件中使用它?
- ios - 在 iOS Safari 中无法获取 WebRTC 的本地候选人
- jinja2 - 在 CKAN 中,是否可以覆盖自定义扩展的模板?
- html - Angular 6 模板 + Bootstrap 4 Accordian 不工作
- url - 查询/url 采用的路由
- asp.net-mvc - PostgreSQL 和 Asp.Net MVC 5:如何以有效的方式向多个用户显示实时数据且限制较少?
- java - 安装 Apache Tomcat 时在选择 Java 路径时显示错误
- dgraph - 查询 - get_all - 多个输入 - Pydgraph
- drupal - Drupal8 菜单项中的中断标签