typescript – Angular 2 – 破坏子组件
发布时间:2020-12-17 07:26:40 所属栏目:安全 来源:网络整理
导读:我从Angular 2开始,我有一个子组件“ChildCmp”初始化,在我需要通过点击销毁组件后,让我们说: @Component({selector: 'main-cmp',templateUrl: './main-cmp.html',directives: [ChildCmp]})class MainCmp { @ViewChild(ChildCmp) childCmp: ChildCmp; destr
|
我从Angular 2开始,我有一个子组件“ChildCmp”初始化,在我需要通过点击销毁组件后,让我们说:
@Component({
selector: 'main-cmp',templateUrl: './main-cmp.html',directives: [ChildCmp]
})
class MainCmp {
@ViewChild(ChildCmp)
childCmp: ChildCmp;
destroyChildClick(){
this.childCmp.destroy();
}
}
但是前面的代码没有运行,destroy()是未定义的,异常是:
我已经阅读了this thread并且使用了ViewContainerRef.createComponent(),使用它创建的组件是“ComponentRef”的实例,但是childCmp没有“ComponentRef”实现. 我如何实现或注入destroy方法? 谢谢大家!
试试这个
export class MainCmp {
@ViewChild(ChildCmp) childRef: ChildCmp;
destroyClick() {
if (this.childRef) {
this.childRef.destroy();
}
}
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
