可以将 Axios 配置为在浏览器页面关闭后保持请求事件吗? native fetch() 接受 keepalive
标记以启用此功能,而我在 Axios 文档中没有找到任何类似的东西。
keepalive The keepalive option can be used to allow the request to outlive the page. Fetch with the keepalive flag is a replacement for the Navigator.sendBeacon() API.
https://developer.mozilla.org/en-US/docs/Web/API/fetch#parameters
最佳答案
是的,如果您创建自己的 axios 实例,您可以使用 axios 执行此操作。
const instance = axios.create({
baseURL: 'https://some-domain.com/api/',
timeout: 1000,
headers: {'X-Custom-Header': 'foobar'},
httpAgent: new http.Agent({ keepAlive: true }),
});
Axios docs 可以为您提供更多指导。
关于javascript - Axios 是否支持与原生 fetch() 相同的 'keepalive' 功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69889711/