[Vuejs]-How to call axios from other folder

0πŸ‘

βœ…

i solve this with

const axios = require("axios");
const instance = axios.create({
  baseURL: process.env.VUE_APP_API_URL,
  headers: {
    "Content-Type": "application/json",
  },
});

exports.create = async () => {
  instance
    .get("/")
    .then((res) => {
      console.log(res);
    })
    .catch((err) => {
      console.log(err);
    })
    .finally(() => {});
};

so, instead import the axios from plugins folder, use it directly from services

0πŸ‘

Well, you’ve defined axios as () => import("../plugins/axios");, so it’s a function, which does not have a .get method. Further, using import as a function makes it return a promise. You need to do:

const axios = import("../plugins/axios");

exports.create = async () => {
  return (await axios)
    .get("/")
      .then((res) => {
        console.log(res);
      })
      .catch((err) => {
        console.log(err);
      })
      .finally(() => {});
};

Leave a comment