[英]jquery ajaxComplete equivalent in axios
Started learning axios and I'm loving it!开始学习 axios,我很喜欢!
Quick question, could not find an answer for it, maybe there is non.快速提问,找不到答案,也许没有。
In jQuery ajax there is a method called ajaxComplete, I was wandering if there is an equivalent in axios?在 jQuery ajax 中有一个叫做 ajaxComplete 的方法,我在想 axios 中是否有一个等效的方法?
axios uses promises. axios 使用承诺。 You can use
您可以使用
axios.get(url,[options]).then(res=>{
/* hande it here */
})
Check out how js promises work if you do not have a basic knowledge https://web.dev/promises/如果您没有基本知识,请查看 js promises 的工作原理https://web.dev/promises/
For global handling axios event this may help https://auralinna.blog/post/2019/global-http-request-and-response-handling-with-the-axios-interceptor/对于全局处理 axios 事件,这可能有助于https://auralinna.blog/post/2019/global-http-request-and-response-handling-with-the-axios-interceptor/
No there is not.不,那里没有。 jQuery's
$.ajax
has a function build in where it fires an event when a request has been finished. jQuery 的
$.ajax
有一个 function 构建,它在请求完成时触发一个事件。 This is the ajaxComplete
event.这是
ajaxComplete
事件。
Axios does not have such behavior, but you could build your own with the CustomEvent
interface. Axios 没有这样的行为,但您可以使用
CustomEvent
接口构建自己的行为。 Or / and assume that there is a method axiosSuccess
on the document
and call that.或者 / 并假设
document
上有一个方法axiosSuccess
并调用它。
const axiosGet = async url => {
try {
const response = await axios.get(url)
const axiosSuccessEvent = new CustomEvent('axiossuccess', {
detail: { url, response }
})
document.dispatchEvent(axiosSuccessEvent)
if (typeof document.axiosSuccess === 'function') {
document.axiosSuccess({ url, response })
}
return response
} catch (error) {
console.log(error)
}
}
And then listen for your own event on the document.然后在文档上监听你自己的事件。
document.addEventListener('axiossuccess', event => {
const { detail } = event
const { url, response } = detail
console.log(url, response)
})
document.axiosSuccess = ({ url, response }) => {
console.log(url, response)
}
axiosGet('https://stackoverflow.com/questions/63998921/')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.