首页 > 解决方案 > 使用 axios 拦截器附加标头

问题描述

我的问题非常具体,我已经搜索过,但没有找到好的答案。我不想简单地通过请求拦截器添加一个标头,但我想附加到它。例如,我有多个服务定义自己的标头并通过配置选项对象传递。但是,我想为这些标头附加一个附加标头,但是当我以以下方式尝试拦截器时,我似乎无法访问现有标头:

axiosIntance.interceptors.request.use((config) => {
    console.log(config); // do not have access to existing header that was added using options 
});

我正在尝试做一些不可能的事情?

标签: vue.jsaxios

解决方案


好吧,我想出了一种方法来做到这一点..它可能对其他人有所帮助..我仍然愿意接受更好的选择..

axiosIntance.interceptors.request.use((config) => {
    config.headers = {'X-Some-Custom-Header': 'custom-header-value', ...config.headers} ;
    // basically the spread operator would preserve the original headers and add to it 
});

推荐阅读