目录问题原因解决问题 在进行 axiOS 封装的时候,遇到个问题,就是每次发起请求时axios 都会执行两次响应拦截,甚是纳闷,一时理不出思路来。 代码如下: class Http
在进行 axiOS
封装的时候,遇到个问题,就是每次发起请求时axios 都会执行两次响应拦截,甚是纳闷,一时理不出思路来。
代码如下:
class Http {
constructor(config) {
this.axios = axios;
this.axiosInterceptor = undefined;
// 公共的 header
let defaultHeaders = {
'Content-Type': 'application/JSON;charset=UTF-8',
'Accept': 'application/json', // 通过头指定,获取的数据类型是JSON 'application/json, text/plain, *
// 添加请求拦截器
axios.interceptors.request.use( (config) => {
// 在发送请求之前做些什么 添加 token 等鉴权功能
//...
return config;
}, function (error) {
// 对请求错误做些什么
return Promise.reject(error);
});
// 添加响应拦截器
axios.interceptors.response.use((res) => {
const { status } = res;
// 在发送结果之前做些什么 对错误状态提示进行处理
//...
return res.data;
}, function (error) {
// 对响应错误做点什么
return Promise.reject(error);
});
class Http {
constructor(config) {
this.axios = axios;
// 这里仍需对配置进行处理,代码省略了
this.config = config;
}
// Get 请求
get(url,params={},headers={}){
// ...
}
// POST 请求
post(url,params={},headers={}){
// ...
}
}
export default Http;
// 无特殊需求的只需使用这个一个对象即可 公共 header 可在此配置, 如需多个实例 可按照此方式创建多个进行导出
export const Axios = new Http({
headers: {
'x-http-token': 'xxx'
}
});
这里不对具体的方法进行描述,只做一个解决问题的说明,后续会针对 axios 类的封装,单独写篇文章再详细说明下的
以上就是axios的interceptors多次执行问题解决的详细内容,更多关于axios interceptors多次执行的资料请关注编程网其它相关文章!
--结束END--
本文标题: axios的interceptors多次执行问题解决
本文链接: https://www.lsjlt.com/news/151727.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-12
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0