javascript
import axios from "axios";
import general from "@/utils/general";
const request =axios.create({
baseURL:general.serverUrl
})
const requestMap =new Map() //保存每一次请求
request.interceptors.request.use(req=>{
let oldRequest =requestMap.get(req.url) //当前请求
if(oldRequest)//是否未响应
{ //取消上一次请求
oldRequest.abort() //取消上次请求
requestMap.delete(req.url) //删除对象
}
const coll = new AbortController()
req.signal=coll.signal
requestMap.set(req.url,coll)//不存在添加当前请求
return req
},)
request.interceptors.response.use(res => {
requestMap.delete(res.config.url)
return res.data
}, error => {
if(error.code=="ERR_CANCELED")//取消后的报错自行处理
{
console.warn('重复的请求')
}else{
return Promise.reject(error)
}
}
)
export default request