【发布时间】:2021-11-01 02:21:24
【问题描述】:
我想使用 Axios 调用一个端点,然后对响应数据和调用前定义的变量做一些事情。我自己创建返回承诺的函数时知道如何pass data into the callback,但由于 Axios 是外部代码,我似乎找不到任何选项在Axios docs 或the config 中传递额外数据。除了将值作为数据发送到服务器并让服务器在响应中响应它或使用window 全局变量使用丑陋的解决方法之外,还有其他方法可以使这项工作吗?
const animate = true; // The variable to be passed
axios.get('/endpoint.json')
.then(function(response, animate) { // This doesn't work...
if (response.data.coordinates) {
console.log(animate); // ...because this is undefined
setLocation('takeoff_location', response.data.coordinates, animate); // variable and response data need to be passed to this call
}
});
【问题讨论】:
-
为什么不直接访问aninate变量,你不需要它作为then()的参数
标签: javascript promise axios scope