Angular 角度2+;创建组件后移除组件

Angular 角度2+;创建组件后移除组件,angular,Angular,我正在动态创建一个组件,我想在1秒后删除它 我想做那样的事 ngOnInit(): void { const time = timer(2000); time.subscribe(() => { REMOVE YOURSELF / THIS COMPONENT }); } 为动态部件的部件支架制定如下指令: import { Directive, ViewContainerRef } from "@angular/core"; @Direc

我正在动态创建一个组件,我想在1秒后删除它

我想做那样的事

  ngOnInit(): void {
    const time = timer(2000);
    time.subscribe(() => {
       REMOVE YOURSELF / THIS COMPONENT
    });
  }

为动态部件的
部件支架
制定如下指令:

import { Directive, ViewContainerRef } from "@angular/core";

@Directive({
    selector: '[component-holder]',
})
export class DynamicComponentDirective {
    constructor(public viewContainerRef: ViewContainerRef) { }
}
使用
ViewChild
将其导入组件,如下所示:

@ViewChild(DynamicComponentDirective ) dynamicComponentDirective : DynamicComponentDirective ;
创建一些动态组件后,您可以按如下方式删除它

this.viewContainerRef = this.dynamicComponentDirective.viewContainerRef;
this.viewContainerRef.clear();

您究竟是如何创建组件的?请提供一些代码。我想使用loopsAny生成它们。如果再次使用
?@Wojtar,请提供一些代码。有几种方法来创建动态组件,甚至不用计算你认为自己是“动态”的。