Angular Http - toPromise 或 subscribe [英] Angular Http - toPromise or subscribe

查看:113
本文介绍了Angular Http - toPromise 或 subscribe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过一些关于 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?

推荐答案

如果您喜欢反应式编程风格并希望在您的应用程序中保持一致以始终使用 observables,即使对于单个事件(而不是事件流),然后使用 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().

observables 的一个优点是,您可以取消请求.

One advantage of observables is, that you can cancel the request.

另见Angular - Promise vs Observable

这篇关于Angular Http - toPromise 或 subscribe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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