美文网首页
axios 拦截器只能添加到实例上

axios 拦截器只能添加到实例上

作者: 滚石_c2a6 | 来源:发表于2017-11-10 15:08 被阅读562次

    axios.interceptors.request.use((config) => {
    config.foo = 123;
    });

    const instance = axios.create();
    instance.interceptors.request.use((config) => {
    config.foo = 456;
    });

    如果没有下面这句,最上面的全局拦截器不会触发,因为只触发了局部的拦截器。
    axios.get('/foo'); // config.foo === 123 -> true

    实例不会继承全局拦截器,参考:https://github.com/axios/axios/issues/993
    instance.get('/foo'); // config.foo === 456 -> true

    相关文章

      网友评论

          本文标题:axios 拦截器只能添加到实例上

          本文链接:https://www.haomeiwen.com/subject/zsvbmxtx.html