首页 > 解决方案 > Vue项目中如何正确实现axios拦截器

问题描述

我正在尝试让 axios 与请求拦截器一起工作。但是,在发出请求之前,不会触发拦截器。这里可能出了什么问题?我已经对这个问题很感兴趣,但到目前为止还没有找到解决方案。在我的 main.js 文件中,我正在使用类似这样的东西。

import App from './App'
import router from './router'



const instance = axios.create({
  baseURL: process.env.VUE_APP_BASE_URL,
  timeout: 10000,
  params: {} // do not remove this, its added to add params later in the config
});

instance.interceptors.request.use(function (config) {
  return config;
},
function (error) {
  // Do something with request error
  return Promise.reject(error)
})

// Add a response interceptor
instance.interceptors.response.use(function (response) {
  // Do something with response data
  return response;
}, function (error) {
  // Do something with response error
  return Promise.reject(error);
});

标签: javascriptvue.jsvuejs2axios

解决方案


推荐阅读