问题描述
我看了几本有关Angular的课程,发现有多种方法可以管理Http请求中的数据.
I have watch a few courses on Angular and have found there are different ways to manage data from an Http request.
- 使用Observables,
.map()
,.subscribe()
- 使用承诺,
.toPromise()
,.then()
,.catch()
我在我的应用程序中使用了 toPromise()
,因为我发现它类似于AngularJS Http服务.
I have used toPromise()
in my application as I find it similar to the AngularJS Http services.
在哪种情况下我需要使用Observables?
In what scenario would I need to use Observables?
推荐答案
如果您喜欢响应式编程风格,并且希望在应用程序中保持一致,即使对于单个事件(而不是事件流)也始终使用可观察对象,那么请使用可观察对象.如果这对您来说无关紧要,请使用 toPromise()
.
If you like the reactive programming style and want to be consistent within your application to always use observables even for single events (instead of streams of events) then use observables. If that doesn't matter to you, then use toPromise()
.
可观察对象的一个优点是,您可以取消请求.
One advantage of observables is, that you can cancel the request.
另请参见 Angular-Promise vs Observable
这篇关于Angular Http-toPromise或订阅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!