vue.js - 使用 axios 拦截器附加标头
问题描述
我的问题非常具体,我已经搜索过,但没有找到好的答案。我不想简单地通过请求拦截器添加一个标头,但我想附加到它。例如,我有多个服务定义自己的标头并通过配置选项对象传递。但是,我想为这些标头附加一个附加标头,但是当我以以下方式尝试拦截器时,我似乎无法访问现有标头:
axiosIntance.interceptors.request.use((config) => {
console.log(config); // do not have access to existing header that was added using options
});
我正在尝试做一些不可能的事情?
解决方案
好吧,我想出了一种方法来做到这一点..它可能对其他人有所帮助..我仍然愿意接受更好的选择..
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
});
推荐阅读
- makefile - Make:在 make 中使用静态模式规则
- javascript - PHP & AJAX - 不刷新以获取数据到另一个页面并使其成为模态
- typescript - 使用 ng-packagr 时 Angular 通用构建失败?
- java - 输入参数为 Map 类型的 Java Spark UDF
- heroku - 在 heroku 中部署时,prisma graphql 令牌无法验证
- hive - 如何通过配置单元在序列文件中写入自定义键值
- jquery - 查找重复的 div 内容并替换它
- python - 在MoviePy中使文本在宽度和高度之间自动调整为TextClip
- java - 生成 TestNG.xml 文件中的问题
- magento2 - Magento结帐表单元素email.html页面编辑问题