admin管理员组文章数量:1315090
I have been using this pattern:
func myObservable() Observable<boolean> {
...
}
func myFunc() {
myObservable().subscribe((cond: boolean) => {
if (cond) {
// How do I unsubscribe here?
}
});
}
However I can't see any way to unsubscribe thereby maybe creating a memory leak.
The reason I ask is because Angular 2's HTTP client uses the same pattern - although I believe it auto-unsubscribes somehow and I would like to do the same.
I have been using this pattern:
func myObservable() Observable<boolean> {
...
}
func myFunc() {
myObservable().subscribe((cond: boolean) => {
if (cond) {
// How do I unsubscribe here?
}
});
}
However I can't see any way to unsubscribe thereby maybe creating a memory leak.
The reason I ask is because Angular 2's HTTP client uses the same pattern - although I believe it auto-unsubscribes somehow and I would like to do the same.
Share Improve this question asked Aug 14, 2016 at 8:20 DanDan 5,3235 gold badges34 silver badges66 bronze badges 1-
1
You need to store the return value which is a
Subscription
and unsubscribe from that. – Jason Goemaat Commented Aug 14, 2016 at 8:21
1 Answer
Reset to default 12You should do something like this:
func myFunc() {
var subscription = myObservable().subscribe((cond: boolean) => {
if (cond) {
// How do I unsubscribe here?
subscription.unsubscribe()
}
});
}
本文标签: javascriptHow to unsubscribe from an RxJS 5 observableStack Overflow
版权声明:本文标题:javascript - How to unsubscribe from an RxJS 5 observable? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741970985a2407836.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论