为什么不推荐使用AngularJS $ http成功/错误方法?从v1.6中删除了? [英] Why are AngularJS $http success/error methods deprecated? Removed from v1.6?

查看:102
本文介绍了为什么不推荐使用AngularJS $ http成功/错误方法?从v1.6中删除了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AngularJS文档已针对 $ http 成功错误方法。

The AngularJS documentation has a Deprecation Notice for the $http success and error methods. Is there a specific reason this abstraction was removed from the library?

推荐答案

问题是 .success是否有特定原因? .error 方法不可链接,因为它们忽略返回值。这给熟悉 chaining 的人带来了问题,并鼓励了不熟悉 chaining 的人提供的劣质代码。见证所有使用延迟反模式的示例。

The problem was that .success and .error methods are not chainable because they ignore return values. This caused problems for people familiar with chaining and encouraged poor code from people unfamiliar with chaining. Witness all the examples on StackOverflow that use the deferred anti-pattern.

要引用AngularJS团队之一:

To quote one of the AngularJS team:


IMO .success .error 首先是API设计的一个缺点。此问题突出表明,在许多情况下,开发人员会感到困惑,因为他们希望。成功 .error 可以正常工作方式为。然后,反之亦然。
在一个完美的世界里,我宁愿放弃这些 $ http 具体的承诺。相反,我们可以鼓励开发人员使用标准的 $ q Promise API .then .catch 。与使用响应对象相比,在使用显式参数方面,IMO没有什么好处。

IMO .success and .error were a bad bit of API design in the first place. This issue highlights a number of situations where developers get confused because they either expect .success and .error to work the same way as .then or vice versa. In a perfect world I would rather just ditch these $http specific "promises". Instead we could encourage developers to use the standard $q promise API .then and .catch. There is very little benefit IMO in working with explicit parameters over working with the response object.

> AngularJS问题#10508 $ http .success / .error .then 的工作方式不同。

— AngularJS Issue #10508 $http .success/.error dissimilar from how .then works.






弃用通知(v1.5)



$ http 遗留的诺言方法成功错误已被弃用。请使用标准的 then 方法。如果 $ httpProvider.useLegacyPromiseExtensions 设置为 false ,则这些方法将抛出 $ http / legacy 错误。

Deprecation Notice (v1.5)

The $http legacy promise methods success and error have been deprecated. Use the standard then method instead. If $httpProvider.useLegacyPromiseExtensions is set to false then these methods will throw $http/legacy error.

— — AngularJS $ http服务API参考-弃用通知








UPDATE



已弃用的。成功 .error 方法已从AngularJS 1.6中删除。


UPDATE

The deprecated .success and .error methods have been removed from AngularJS 1.6.


由于 b54a39 $ http 弃用的自定义回调方法- .success() .error()-已被删除。您可以使用标准的 .then() / .catch() Promise方法,但请注意,方法签名并且返回值是不同的。

Due to b54a39, $http's deprecated custom callback methods - .success() and .error() - have been removed. You can use the standard .then()/.catch() promise methods instead, but note that the method signatures and return values are different.

$http(...)
  .then(function onSuccess(response) {
    // Handle success
    var data = response.data;
    var status = response.status;
    var statusText = response.statusText;
    var headers = response.headers;
    var config = response.config;
    ...
  }).catch(function onError(response) {
    // Handle error
    var data = response.data;
    var status = response.status;
    var statusText = response.statusText;
    var headers = response.headers;
    var config = response.config;
    ...
  });

— AngularJS开发人员指南-迁移至v1.6-http

这篇关于为什么不推荐使用AngularJS $ http成功/错误方法?从v1.6中删除了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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