VUE—axios自定义请求配置—timeout

1,142 阅读1分钟

在项目中使用axios肯定会使用到timeout属性, 常见的写法

(1) axios.defaults.timeout = 5000;

(2)let instance= axios.create({undefined baseURL: "", timeout: 5000, });

这里的timeout的作用其实就是,设置了在5000毫秒内请求数据 如果没有请求成功就执行错误函数,这里用axios实例举例 如:

let instance = axios.create({
    baseURL: "https://xxxxxxxx",
    tiemout: 5000
});
     // 链接超时  当发送时间超过5秒就不再发送了
    // 若网速过慢发送不成功就会报错
 
instance ({
    url: "/getAll",
    methods: "get",
}).then(data => {
    console.log(data);
}).catch(err => {
    console.log(err);
})

// 如果到达5000毫秒未请求到接口的数据就会执行catch中的语句