【发布时间】:2019-02-18 10:15:12
【问题描述】:
我使用以下方法创建了很多动态组件:
this.factory = this.componentFactoryResolver.resolveComponentFactory(SliderComponent);
this.sliderComponentReference = viewContainerRef.createComponent(this.factory);
当我需要销毁组件时,我调用 destroy 方法:
this.sliderComponentReference.destroy();
我知道它从视图中删除了动态组件并且它是实例但是当我在我注意到它仍然有信息之后立即查看变量时:
changeDetectorRef: ViewRef_ {_view: {…}, _viewContainerRef: ViewContainerRef.. }
componentType:(...)
hostView: ViewRef_ {_view: {…}, _viewContainerRef: ViewContainerRef... }}
injector:(...)
问题:
如果变量被销毁了,怎么还有对组件实例的引用?
组件是否仍存储在内存中?如果可以,是否可以检索?
【问题讨论】:
-
“查看变量之后”是指
this.sliderRef.destroy(); console.log(this.sliderRef)? -
我创建了这个StackBlitz Demo,所以任何有兴趣的人都可以看到 OP 所指内容的运行代码示例。
标签: angular typescript reference components destroy