我从Angular 2开始,我有一个子组件"ChildCmp"初始化,在我需要通过点击销毁组件之后,让我们说:
@Component({
selector: 'main-cmp',
templateUrl: './main-cmp.html',
directives: [ChildCmp]
})
class MainCmp {
@ViewChild(ChildCmp)
childCmp: ChildCmp;
destroyChildClick(){
this.childCmp.destroy();
}
}
Run Code Online (Sandbox Code Playgroud)
但是前面的代码没有运行,destroy()是未定义的,异常是:
TypeError:this.childCmp.destroy不是函数
我已经阅读了这个线程并且使用了ViewContainerRef.createComponent(),使用它创建的组件是"ComponentRef"的实例,但是childCmp没有"ComponentRef"实现.
我如何实现或注入destroy方法?
谢谢大家!