立即取消订阅RxJS Observable [英] Immediately unsubscribing from RxJS Observable

查看:114
本文介绍了立即取消订阅RxJS Observable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以阐明我将如何立即取消订阅RxJS的方式吗?可观察到的是来自Angular 2 EventEmitter.收到活动后,我想取消订阅.这里的问题是在功能块的内部中取消订阅.我觉得这是错误的方法:

Could anyone shed some light on how I would immediately unsubscribe from an RxJS subscription? The observable is from Angular 2 EventEmitter. When I recieve the event, I want to cancel the subscription. The issue here is cancelling the subscription within the function block. I have a feeling that this is the wrong approach:

this.subscription = observable.subscribe(result => {
    // do something
    // **unsubscribe**
});

推荐答案

observable.first().subscribe(....)

在第一个事件之后将终止订阅.

will end the subscription after the first event.

RxJs 6+的更新

observable.pipe(first()).subscribe(....)

这篇关于立即取消订阅RxJS Observable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆