### 关于 axios 拦截器链式调用的一个注意点

关于 axios 拦截器链式调用的一个注意点

const ins = axios.create({
        baseURL: 'https://www.showdoc.com.cn/server/api/user/login', //基础地址
        timeout: 1000, //超时时限
    })
    // 请求拦截器
    ins.interceptors.request.use(config => {
        console.log('send the request')
        return config
    })
     // 请求拦截器2
    ins.interceptors.request.use(config => {
        console.log('send the request2')
        return config
    })
    // 响应拦截器
    ins.interceptors.response.use(config => {
        console.log('send the response')
        return config
    })
    // 响应拦截器2
    ins.interceptors.response.use(config => {
        console.log('send the response2')
        return config
    })
    // 两个axios实例
    const res1 = await ins.get('')
对于上述代码,控制台响应的结果是:
send the request2
send the request
send the response
send the response2
我们可以注意到,axios 的请求拦截器和响应拦截器的执行顺序不同:
1. 请求拦截器是逆序返回结果
2. 响应拦截器是顺序返回结果
3. 所以我们在编写 axios 拦截的时候,需要考虑拦截器的编写顺序
看到这里,点个赞呗
  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值