[英]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.
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. 首先,来自Angular中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. 最后,如果您仍然找不到
takeWhile
或takeUntil
,那么另一个潜在的小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.