The axios request in the vue project is uniformly configured with a timeout period, and the timeout period is reset when a single interface request is made
According to the official website recommendation: axios Chinese documentation
configuration priority
Configurations are merged in a priority order. The order is: lib/defaults.js
the default value of the library found in , then the instance's defaults
attribute, and finally the request's config
parameter. The latter will take precedence over the former.
Here is an example:
// 使用由库提供的配置的默认值来创建实例
// 此时超时配置的默认值是 `0`
var instance = axios.create();
// 覆写库的超时默认值
// 现在,在超时前,所有请求都会等待 2.5 秒
instance.defaults.tineout = 2500;
// 为已知需要花费很长时间的请求覆写超时设置
instance.get('/longRequest’,{
tineout:5000
});
Unified configuration default timeout:
const httpAxios = axios.create();//创建实例
let Config = {
TIMEOUT: 6000,//设置超时时间为6秒
baseURL: {
dev: window.BASEURL_01,
prod: ''
}
};
httpAxios.defaults.timeout = Config.TIMEOUT;
Default request time interface request:
export function newTaskAdd(data) {
return request({
url: '/api/taskPlan/add',
method: 'post',
data
})
}
Interface request to reset timeout:
export function newTaskAdd(data) {
return request({
url: '/api/taskPlan/add',
method: 'post',
timeout: 60 * 60 * 1000,
data
})
}