• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

cokelike

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

axios 配置

axios拦截器:

 

import axios from "axios"
 
//请求拦截器    
var symbol
axios.interceptors.request.use((config)=>{
    if(typeof symbol==='function'){ //取消未完成请求
        symbol('取消请求')
     }
     config.cancelToken=new axios.CancelToken((a)=>{ //从新创建请求a
        symbol=a
     })
     return config
    },error=>{
        return Promise.reject(error) 
})
  
//响应拦截器
axios.interceptors.response.use(
    response=>{
        symbol=null     //将请求置空
        return response //返回响应结果
    },
    error=>{
        if(axios.isCancel(error)){ //传递取消错误
            return new Promise(()=>{}) //中断Promise连接
        }else{ //请求出错
            symbol=null
            return Promise.reject(error) //将错误传递下去 throw error
        }
    }
)
export default axios

  

https://github.com/axios/axios

posted on 2022-09-10 17:32  cokelike  阅读(26)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3