axios配置分为 全局配置(默认配置),实例配置,请求配置
以实例配置为例:
创建一个axios实例
axios.create({
//以下是常用的配置属性
baseURL:'', //请求的域名,请求地址 例如:'http://localhost:8080'
timeout:'', //请求超时时间,超过此时间将返回401,(ms),时间过长会阻塞后端传输的数据
url:'', //请求的路径
method:'', //请求的方法(get,post,put,patch,delete等)
headers:'', //设置请求头
params:{}, //请求参数拼接在url上
data:{} //请求阐述拼接在请求体中
})
- 全局配置/默认配置
修改参数:
axios.defaults.属性名 = 属性值;
例如:
axios.defaults.timeout = 2000;
- 实例配置
创建实例:
let instance = axios.create({
baseURL:'http://localhost:8080',
timeout:1000
})
修改:
instance.defaults.属性名 = 属性值;
- 请求配置
请求中修改参数timeout
instance.get('/get',{
timeout:2000
}).then((res)=>{
console.log(res);
})
优先级
全局配置 < 实例配置 < 请求配置