javascript - 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);
});
解决方案
推荐阅读
- c# - 如何更改图像填充量方向?
- javascript - 按钮不适用于 JS 插入的 HTML,但适用于纯 HTML
- python - Python spaCy - 参数'string'的类型不正确(预期的str,得到了DataFrame)
- r - What's the correct way to implement a user-defined do.call's what function?
- r - 如何在循环中创建向量
- javascript - 根据选中的复选框显示/隐藏部分
- c - 创建使用递归的程序时遇到问题
- matlab - matlab中的三重求和
- javascript - 如何设置 setInteval 使其适用于特定的子类
- php - 如果是子域,则在 Laravel 5.7 中调用子域 URL 否则域 URL 调用