最终回调中的axios响应对象未定义

时间:2019-02-07 08:35:49

标签: javascript vuejs2 axios es6-promise

我在我的Vue.js项目之一中使用axios。无论API调用失败还是完成,我都在执行一些操作。我开始了解finnaly()方法。 API请求失败或成功后,将执行该命令。   但是我在最终给定的回调中没有得到响应对象。

例如:

axios()
.then((response) => {
  console.log(response); // response object defined
  //handle response on success
  return response
}).finally((response) => {
  console.log(response); // response object undefined
});

2 个答案:

答案 0 :(得分:2)

Promise.finally方法没有为回调提供参数

读取MDN [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/finally]

您可以再次使用.then来获取响应。

答案 1 :(得分:0)

我通过包含polyfill.finally脚本解决了该问题:

<script src="https://polyfill.io/v3/polyfill.min.js?features=Promise.prototype.finally" defer></script>