0

I am setting default headers in my vue application, but when I load the app it show me error.

main.js?56d7:14 Uncaught TypeError: Cannot read property 'defaults' of undefined

enter image description here

Code

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

1 Answer 1

1

I made a working codepen based on your issue. In your case, you need to create axios client. Please check this 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;
Sign up to request clarification or add additional context in comments.

1 Comment

Thanku @endmaster0809 for this work, you save my day

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.