我从Angular 2开始,我有一个子组件“ChildCmp”初始化,在我需要通过点击销毁组件后,让我们说:
@Component({ selector: 'main-cmp',templateUrl: './main-cmp.html',directives: [ChildCmp] }) class MainCmp { @ViewChild(ChildCmp) childCmp: ChildCmp; destroyChildClick(){ this.childCmp.destroy(); } }
但是前面的代码没有运行,destroy()是未定义的,异常是:@H_502_4@
TypeError: this.childCmp.destroy is not a function@H_502_4@
我已经阅读了this thread并且使用了ViewContainerRef.createComponent(),使用它创建的组件是“ComponentRef”的实例,但是childCmp没有“ComponentRef”实现.@H_502_4@
谢谢大家!@H_502_4@
试试这个
export class MainCmp { @ViewChild(ChildCmp) childRef: ChildCmp; destroyClick() { if (this.childRef) { this.childRef.destroy(); } } }