Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

569
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda