gpt4 book ai didi

axios - 如何在代码中的一个位置记录所有 axios 调用

转载 作者:行者123 更新时间:2023-12-03 12:55:56 30 4
gpt4 key购买 nike

我正在将 axios 用于 React 应用程序,并且我想记录我在应用程序中任何位置进行的所有 axios 调用。我已经通过 create 函数使用了 axios 的单个全局实例,并且我能够记录通用的 console.log。但是我想要更多信息,例如被调用的函数、参数等。

最佳答案

做到这一点的最好方法是拦截器。每个拦截器在请求/响应之前被调用。在这种情况下,日志拦截器就是。

axios.interceptors.request.use(request => {
console.log('Starting Request', JSON.stringify(request, null, 2))
return request
})

axios.interceptors.response.use(response => {
console.log('Response:', JSON.stringify(response, null, 2))
return response
})

或者类似的东西。

很高兴您使用 axios 的新实例:

const api = axios.create({
timeout: 1000
})

这样你就可以打电话了

api.interceptors[...]

关于axios - 如何在代码中的一个位置记录所有 axios 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41751235/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com