首页 > 解决方案 > 无法将 Axios 默认标头添加到 VueJS 应用程序

问题描述

所以我试过了

axios.defaults.headers.common['X-CSRF-TOKEN'] = 'test';
axios.defaults.headers.common['Authenticaton'] = 'Bearer slskdf';

在我的路由器文件中的main.jsindex.js中,beforeEach ..... 浏览器中根本没有出现标题。

我做错了什么让这些标题出现在每次通话中?

谢谢

标签: vue.jsaxioshttp-headersjwt

解决方案


从 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]

推荐阅读