vue.js - 无法将 Axios 默认标头添加到 VueJS 应用程序
问题描述
所以我试过了
axios.defaults.headers.common['X-CSRF-TOKEN'] = 'test';
axios.defaults.headers.common['Authenticaton'] = 'Bearer slskdf';
在我的路由器文件中的main.js和index.js中,beforeEach ..... 浏览器中根本没有出现标题。
我做错了什么让这些标题出现在每次通话中?
谢谢
解决方案
从 0.19 版本开始,如果您创建 axios 实例来发出请求,axios 将不再使用 axios.defaults。为实例设置默认值并使用它来发出请求。
const adapter = axios.create({
baseURL: 'http://localhost:8080/my-api',
headers: {
Accept: 'application/json;charset=UTF-8',
},
});
adapter.defaults.headers.common['Authorization'] = 'Bearer test';
adapter.get(...)
See [this issue][1] and [related pull request][1]
推荐阅读
- java - 如何避免在这个作业中使用 instanceof?
- html - 防止来自 django tinymce 的危险用户输入
- bash - 从 bash 导出函数并通过命令行运行它
- python - 我在 GitHub 的这个 Python 示例代码中做错了什么?
- c++ - 调用函数时未定义标识符“function_name”
- android - 存储访问框架音频选项卡“媒体是只读的”
- php - 用于以特殊模式添加空格的正则表达式
- javascript - 将 Flow 与 ImmutableJs 记录一起使用 - 缺少“值”的类型注释
- python - WebDriver 位置循环
- kotlin - JUnit 测试中的 Kotlin 内联类