if
(!
response.data.hasError) {
this
.$message.success("数据获取成功!"
);
const results
=
response.data.result;
}
else
{
this
.$message.error("数据获取失败!"
);
.
catch
((error) =>
{
clearTimeout(timer);
if
(error.response) {
this
.$message.error("数据获取失败!"
);
注意: 可以使用同一个 cancel token 取消多个请求
二、设置超时时间,超时重新请求
配置axios拦截器
axios.interceptors.response.use(undefined, function axiosRetryInterceptor(err) {
var config = err.config;
// If config does not exist or the retry option is not set, reject
if(!config || !config.retry) return Promise.reject(err);
// Set the variable for keeping track of the retry count
config.__retryCount = config.__retryCount || 0;
// Check if we've maxed out the total number of retries
if(config.__retryCount >= config.retry) {
// Reject with the error
return Promise.reject(err);
// Increase the retry count
config.__retryCount += 1;
// Create new promise to handle exponential backoff
var backoff = new Promise(function(resolve) {
setTimeout(function() {
resolve();
}, config.retryDelay || 1);
// Return the promise in which recalls axios to retry the request
return backoff.then(function() {
return axios(config);
参数说明:
1.retry -
第一次请求失败后重试请求的次数。
2.retryDelay -
在失败请求之间等待的毫秒数(默认为1)。
axios.get('/some/endpoint', { retry: 5, retryDelay: 1000 })
.then(function(res) {
console.log('success', res.data);
.catch(function(err) {
console.log('failed', err);
参考:https://www.jianshu.com/p/e58ff85b2a91