angular - 何时以角度取消订阅 SSE 事件源事件(关闭/刷新案例)
问题描述
我在我的应用程序的服务中使用了一些 EventSource。
我想在浏览器关闭/刷新时关闭所有连接。
我看过这个帖子
现在,这就是我添加到我的服务中的内容。
@HostListener('window:beforeunload')
onBrowserClose() {
this.clearAll();
}
clearAll() {
if (this.eventSource) {
this.eventSource.close();
}
this.callbacks = new Map<string, (e: any) => void>();
}
有更好的解决方案吗?
解决方案
您可以ngOnDestroy()
用于取消订阅事件
ngOnDestroy() {
if (this.eventSource) {
this.eventSource.close();
}
this.callbacks = new Map<string, (e: any) => void>();
}
我想在浏览器关闭/刷新时关闭所有连接。
this angular life cycle method will be called when component is going to destroy
推荐阅读
- amazon-web-services - 新 AWS 云 HSM 中的 PKCS Luna 支持
- npm - 无法启动 webpack-dev-server
- raspberry-pi - Odoo - PosBox 少打印机断开连接
- laravel - Laravel 测试:协调 http-request 和控制器
- angular - AG-Grid Angular 6 m 多线接头
- arrays - 随机混合两个 Numpy 数组
- c# - ldap - 未找到网络路径
- tensorflow - 如果输入被tf.data.TFRecordDataset随机打乱,Tensorflow如何获得输入和输出之间的映射关系
- spring - Spring中特定咖啡因缓存的配置
- android - 使用 getter、setter 和适配器从 firebase 中的特定节点/子节点检索数据,详情请参阅