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

En español

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.

!Courses from!

10$

On Udemy

There are 4d 18:02!


Udemy

!Courses from!

4$

In Academy

View courses

!Books from!

1$

See the books
¡Become an affiliate on Gumroad!