Setting axios for a project in Vue 3

- Andrés Cruz

En español
Setting axios for a project in Vue 3

Axios has become our best partner for making http requests using JavaScript; and in Vue, it is not the exception; learn how to set it up.

In order to use axios globally in Vue; from the root file, which creates the main instance of Vue:

In the main.js:

import axios from 'axios'
import App from "./App.vue"
const app = createApp(App).use(Oruga)
app.config.globalProperties.$axios = axios
window.axios = axios

As you can see, what we do first is import axios (assuming you already have it installed) and set it to a global Vue property; therefore, when we want to use it in any Vue component, we can use something like the following:

 this.$axios.get("/api/post").then((res) => {
   // ***
Andrés Cruz

Desarrollo con Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz en Udemy

Acepto recibir anuncios de interes sobre este Blog.