gpt4 book ai didi

angular - 组件可以调用自毁事件吗

转载 作者:太空狗 更新时间:2023-10-29 16:56:01 25 4
gpt4 key购买 nike

我有一个父组件,它在单击链接时打开一个新组件,这个新组件应该有一个关闭按钮,该按钮在关闭时向父组件发送关闭消息并自行销毁。

我们可以使用 ngOnDestroy 方法发送关闭消息,但我如何调用子组件的销毁。

<parent>
<child></child> //child to be opened on click but close
//event should be inside the child componenet
</parent>

如果我在这里有一些概念上的错误,请纠正我。谢谢

最佳答案

如果您使用 ViewContainerRef.createComponent() 添加组件,如 Angular 2 dynamic tabs with user-click chosen components 所示,那么当您将 cmpRef 传递给创建的组件时,组件可以自行销毁。

否则我觉得没有办法。您可以将一个值传递给父级,以便 *ngIf 删除该组件。

<child *ngIf="showChild" (close)="showChild = false"></child>
class ParentComponent {
showChild:boolean = true;
}
class ChildComponent {
@Output() close = new EventEmitter();

onClose() {
this.close.emit(null);
}
}

关于angular - 组件可以调用自毁事件吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39764546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com