vue网络不好时不间断请求

  • 配置默认参数
const { apiConfig: { timeout, retry, retryDelay } } = config;

if(timeout) axios.defaults.timeout = timeout;     //超时时间 
if(retry) axios.defaults.retry = retry;    //请求次数
if(retryDelay) axios.defaults.retryDelay = retryDelay; //请求间隙
  • 路由拦截
const errConfig = error.config || {};
if(error.code == 'ECONNABORTED' && ~error.message.indexOf('timeout') && errConfig.retry){
    errConfig.__retryCount = errConfig.__retryCount || 0;
    errConfig.__retryCount += 1;
    if(errConfig.__retryCount < errConfig.retry) {
        return that.request(errConfig) //再次请求
    }
}

this.destroy(url);
if(~error.message.indexOf('timeout')) msg = '系统请求超时,请稍后重试';
tipsMessage(msg);
return false;
原文地址:https://www.cnblogs.com/ajaemp/p/11906026.html