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
Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter