axios的攔截器(Interceptors)


  axios 的攔截器:interceptors

  如果我們想在請求之前做點什么,用攔截器再好不過了

  攔截器一般做什么?

    1. 修改請求頭的一些配置項

    2. 給請求的過程添加一些請求的圖標

    3. 給請求添加參數

  1. 全局的攔截器配置

  

  代碼: 

  axios.interceptors.request.use(config=>{
    console.log(1234);
    return config
  },err=>{
    console.log(err)
  })
  axios({
    url:"./json/01.json",
    method:"get"
  }).then(res=>{
    console.log(res);
  })

  2. 局部的攔截器

  

  代碼:  

  let instance = axios.create({
    baseURL:"./json/",
    timeOut:5000
  });
  instance.interceptors.request.use(config=>{
    console.log(1234);
    return config
  },err=>{
    console.log(err)
  })
  instance({
    url:"/01.json",
    method:"get"
  }).then(res=>{
    console.log(res)
  })

   


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM