如何解决无法读取vue中undefined的属性'defaults'?

b1payxdu  于 7个月前  发布在  Vue.js
关注(0)|答案(1)|浏览(69)

我在我的vue应用程序中设置了默认头,但当我加载应用程序时,它显示错误。
main.js?56d7:14 Uncaught TypeError:无法读取未定义的属性'defaults'


的数据

验证码

const token = sessionStorage.getItem("token");
if (token) {
  Vue.prototype.$http.defaults.header.common["Authorization"] = token;
}

字符串

ddrv8njm

ddrv8njm1#

我根据你的问题做了一个工作codepen。在你的情况下,你需要创建axios客户端。请检查这个codepen。https://codepen.io/plsdev89/pen/VwaPGzr

let apiClient = axios.create({
   baseURL: "https://jsonplaceholder.typicode.com",
   headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json'
   }
})

const token = "testtoken";
if (token) {
   apiClient.defaults.headers.common["Authorization"] = token;  
}

Vue.prototype.$http = apiClient;

字符串

相关问题