简体   繁体   English

如何使用角度4中的条件取消订阅

[英]How to unscribe from a subscription using a condition in angular 4

How do I unsubscribe to a subscription with a condition in typescript. 如何退订带有打字稿中条件的订阅。

I subscribed to a $http.get method in Angular4, now I want to unsubscribe after received "no_more_data" from the server. 我在Angular4中订阅了$ http.get方法,现在我想在从服务器收到“ no_more_data”后退订。 Please let me know how to do it. 请让我知道该怎么做。

All the methods that I know like takeuntil, takewhile etc are not available on this Observable 我知道的所有方法(例如takeuntil,takewhile等)在此Observable上均不可用

getFiles(): any {
    return http.post
}

getFiles(). // here no takeWhile and takeUntil are getring allowed then how do I unsubscribe.

You can use the takeWhile operator to make the stream close automatically when "no_more_data" arrives: 您可以使用takeWhile运算符在"no_more_data"到达时自动关闭流:

$http.get(...)
.map(...)
.takeWhile((e) => e != "no_more_data")
.subscribe(...)

See also takeWhile 另请参阅takeWhile

There're a few things here: 这里有几件事:

First, the observable that comes from Http in Angular makes a single Http request, so, no need to unsubscribe if it's just a single Http call, unless you are triggering it from something else that is repeated of course. 首先,来自A​​ngular中Http的可观察对象发出单个Http请求,因此,如果只是单个Http调用,则无需取消订阅,除非您从其他重复的过程中触发了它。

Second, the part that you mention, return http.post is obviously incomplete. 其次,您提到的return http.post部分显然是不完整的。 I don't know whether this is just for posting in the question or your real code is like that. 我不知道这是仅用于发布问题还是您的真实代码就是这样。 Your problem might be that you are returning the function http.post instead of calling it. 您的问题可能是您返回了函数http.post而不是调用它。

Check if your real code 1) calls the method http.post( /*some arguments here /*) , and of course 2) does return the result of that call. 检查您的实际代码是否1)调用方法http.post( /*some arguments here /*) ,当然2)确实返回了该调用的结果。 Hover over your method that calls http.post() to ensure the return type is an Observable . 将鼠标悬停在调用http.post()方法上,以确保返回类型为Observable

Finally, another potential small gotcha if you are unable to find the takeWhile or takeUntil still, is that it might simply be not imported. 最后,如果您仍然找不到takeWhiletakeUntil ,那么另一个潜在的小takeUntil是,它可能根本不会被导入。 You might need to add import 'rxjs/add/operator/takeWhile or `'rxjs/add/operator/takeUntil`` to the beginning of the file that uses it. 您可能需要将import 'rxjs/add/operator/takeWhile或`'rxjs / add / operator / takeUntil'添加到使用它的文件的开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular如何从成功订阅退回到错误? - Angular How to fallback to error from successfull subscription? 如何在Angular中取消订阅未初始化的订阅 - How to unsubscribe from not initialized subscription in Angular 如何在某些情况下使用angular 4从sheetjs中删除列? - How to delete a column from sheetjs with some condition using angular 4? 如何以角度返回订阅 - How to return a subscription in angular 如何使用 Apollo Angular 测试查询、变异、订阅服务? - How cant test Query, Mutation, Subscription services using Apollo Angular? 如何枚举Angular 2中从Observable Subscription返回的数据? - How to enumerate data returned from Observable Subscription in Angular 2? 如何在Angular 2/4中返回来自响应的数据而不是订阅json - How to return data coming from response instead of subscription json in Angular 2/4 如何从角度中的单个函数返回订阅和承诺? - How to return subscription and promise from single function in angular? 如何在不使用异步等待的情况下以角度获取订阅内的价值 - How to get value inside the subscription in angular without using async await 如何在订阅期间从可观察的Angular 5 RxJS中检查NULL结果? - How to Check for a NULL Result from an Angular 5 RxJS Observable during Subscription?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM