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

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

问题描述

AngularJS 文档对 $http successerror 方法有弃用通知.从库中删除此抽象是否有特定原因?

解决方案

问题在于 .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 遗留的承诺方法 successerror 已被弃用.请改用标准的 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 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 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 Developer Guide - Migrating to v1.6 - http

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

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