Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

566
Views
Axios interceptors with Vue 3 and Vite not working

I have a problems with axios, in my code not execute the functions of my plugin, i am use vite.I can't bind my token in the headers.

interceptor.ts

import type { App } from "vue";
import axios from "axios";

export default {
  install: (app: App): void => {
    app.config.globalProperties.$http = axios;
    const $http = app.config.globalProperties.$http;
    
    const handleResponse = (response: unknown) => {
      // ! this does not run 
      console.log('response :>> ', response);
      return response;
    };
    
    const handleError = (err: unknown) => {
      // ! this also does not
      console.log('err :>> ', err);
      return err;
    }
    $http.interceptors.response.use(handleResponse, handleError);
  },
};

main.ts

import { createApp } from "vue";
import { createPinia } from "pinia";

import element_plus from "@/plugins/element-ui";
import interceptor from "@/plugins/interceptor";

import App from "./App.vue";
import router from "./router";

const app = createApp(App);

app.use(element_plus);
app.use(interceptor);
app.use(createPinia());
app.use(router);

app.mount("#app");

export default app;

package.json

"dependencies": {
    "@bundled-es-modules/axios": "^0.27.2",
    "boxicons": "^2.1.2",
    "element-plus": "^2.2.0",
    "pinia": "^2.0.13",
    "vue": "^3.2.33",
    "vue-router": "^4.0.14"
  },

I have tried redaxios but without success. I can help me with a example?, thanks.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Solution

I found the solution, the key is to create an instance and make the export.

common-http.ts

import axios from "axios";
const baseURL = import.meta.env.VITE_BASE_API;

const apiClient = axios.create({
  baseURL,
  headers: {
    "Content-type": "application/json",
    "Access-Control-Allow-Origin": "*",
  },
  validateStatus: () => true,
});

export default apiClient;

then, I call the function on my interceptor.

import type { App } from "vue";
import type { AxiosResponse } from "axios";
import http from "@/utils/common-http";

export default {
  install: (app: App): void => {
    app.config.globalProperties.$http = http;
    const $http = app.config.globalProperties.$http;

    const handleResponse = (response: AxiosResponse) => {
      // Here your code
      console.log('response :>> ', response);
      return response;
    };
    ...
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!