axios 重新发起请求,一般用于重新发起请求
1.切换域名重试:
const response = error.response
const config = response.config
config.baseURL = api_2
console.log('err', error)
return new Promise((resolve, reject) =>
setTimeout(() => {
try {
resolve(axios(config))
} catch (err) {
reject(err)
}
}, 1 * 1000))
2.重试次数控制:
//响应拦截器 axios.interceptors.response.use((response) => { const res = response.data if (res.code == 10000) { return res.data //return Promise.resolve(res.data) } else { console.warn(res.msg); return Promise.reject(res) } }, (error) => { const response = error.response const config = response.config const code = response.status.toString().substr(0, 1) if (code == "4" && api_count > 0) { api_count-- } else { if (config.baseURL == api_2) { return } config.baseURL = api_2 } const $throw=Vue.prototype.$throw $throw(error) return new Promise((resolve, reject) => setTimeout(() => { try { resolve(axios(config)) } catch (err) { reject(err) } }, 1 * 1000)) })