为什么不推荐使用 AngularJS $http 成功/错误方法?从 v1.6 中删除? [英] Why are AngularJS $http success/error methods deprecated? Removed from v1.6?
问题描述
AngularJS 文档对 $http
success
和 error
方法有弃用通知.从库中删除此抽象是否有特定原因?
问题在于 .success
和 .error
方法不可链接 因为它们忽略返回值.这给熟悉链的人带来了问题,并鼓励不熟悉链的人编写糟糕的代码.见证 StackOverflow 上所有使用 延迟反模式的示例.
引用 AngularJS 团队的一位:
<块引用>IMO .success
和 .error
最初是 API 设计的一个糟糕部分.这个问题突出了许多开发人员感到困惑的情况,因为他们要么期望 .success
和 .error
以与 .then
相同的方式工作,要么反之亦然.在一个完美的世界中,我宁愿放弃这些 $http
特定的承诺".相反,我们可以鼓励开发人员使用标准的 $q
promise API .then
和 .catch
.与使用响应对象相比,使用显式参数对 IMO 的好处很小.
—AngularJS 问题 #10508 $http
.success/.error
与 .then
的工作方式不同.
<块引用>
弃用通知 (v1.5)
$http
遗留的承诺方法 success
和 error
已被弃用.请改用标准的 then
方法.如果 $httpProvider.useLegacyPromiseExtensions
设置为 false
,那么这些方法将抛出 $http/legacy
错误.
—AngularJS $http 服务 API 参考-- 弃用通知
<小时>
更新
已弃用的 .success
和 .error
方法已从 AngularJS 1.6 中删除.
由于 b54a39,$http
's 已弃用的自定义回调方法 - .success()
和 .error()
- 已被删除.您可以改用标准的 .then()
/.catch()
承诺方法,但请注意,方法签名和返回值是不同的.
$http(...).then(function onSuccess(response) {//处理成功var data = response.data;var status = response.status;var statusText = response.statusText;var headers = response.headers;var config = response.config;...}).catch(函数onError(响应){//处理错误var data = response.data;var status = response.status;var statusText = response.statusText;var headers = response.headers;var config = response.config;...});
——AngularJS 开发者指南——迁移到 v1.6 - 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?
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.
To quote one of the AngularJS team:
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 Issue #10508
$http
.success/.error
dissimilar from how.then
works.
Deprecation Notice (v1.5)
The
$http
legacy promise methodssuccess
anderror
have been deprecated. Use the standardthen
method instead. If$httpProvider.useLegacyPromiseExtensions
is set tofalse
then these methods will throw$http/legacy
error.— AngularJS $http Service API Reference -- deprecation notice
UPDATE
The deprecated .success
and .error
methods have been removed from AngularJS 1.6.
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 $http 成功/错误方法?从 v1.6 中删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!