axios 使用与 拦截器

未拦截使用使用:

由于axios每个请求都是异步.例如有ABC三个请求,C需要拿到AB请求回来的值作为参数,则需同步加载才能,所以使用axios.all才好完成....

拦截器:为了处理axios中get(params:)和post(data:)不同..还有就是只要拿到自己想要的data,

import axios from "axios"
export serve = axios.create({
timeout:5000,
withCredentials:true
})

serve.interceptors.request.use((config)=>{
if (config.method=="post") {
config.data=config.data;
} else if(config.method=="get"){
config.params = {...config.data}
}
return config;
},(e)=>{
Promise.reject(e)
})

serve.interceptors.response.use((config)=>{
if (res.status == 200) {
return res.data
}
},(e)=>{
return Promise.reject(e)
})

 在使用

猜你喜欢

转载自www.cnblogs.com/kaijiangyugty/p/11146269.html