I created a new project using:
vue create hello-world
Generating a new project that includes the HelloWorld.vue
, app.vue
, main.js
(etc ...) files.
Now I install Axios by following the docs Npm vue-axios:
npm install --save axios vue-axios
I import Axios in the main.js
file:
import Vue from 'vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
And now I run into a problem that I don't understand. The VueAxios
docs say you simply use it like so:
const app = Vue.createApp(...)
app.use(VueAxios, axios)
But the way app
is created in Vue 3 is different. I think this is where the problem comes from:
createApp(App).mount('#app')
So, how do I correctly import axios?
in Vue 3. This wrapper bind axios to app instance or this if you're using single file component.
Axios is a library for http communication, making ajax requests, and so on. There is also a library called 'vue-resource' to do such things, but it's said that it is not used well due to it's slow update cycle and larger community of Axios.
Installing and Importing axios. To begin, you must install Axios. You can install Axios with npm: npm install axios --save Or with Yarn: yarn add axios When adding Axios to your Vue.js project, you will want to import it: import axios from 'axios'; Next, we will use axios.get() to make a GET request. Populating Data with a GET Request
β There are 3 components: TutorialsList, TutorialDetails, AddTutorial. β router.ts defines routes for each component. β http-common.ts initializes axios with HTTP base Url and headers. β TutorialDataService has methods for sending HTTP requests to the Apis. β vue.config.js configures port for this Vue Client.
tip If youβre working in VSCode, you can go to Terminal > New Terminal to open a terminal or command prompt with the project folder already selected. Alternatively, you can use yarn or bower . Once the installation is complete, you should see Axios in the list of dependencies in your package.json file.
There is a window object available to you in the browser. You can actively leverage that based on your requirements. import axiosApi from 'axios'; const axios = axiosApi.create ( { baseURL:`your_base_url`, headers: { header:value } }); //Use the window object to make it available globally. window.axios = axios; Now in your component.vue
createApp(App).mount('#app')
is effectively the same as:
import Vue from 'vue'
const app = Vue.createApp(App)
app.mount('#app')
// or
import { createApp } from 'vue'
const app = createApp(App)
app.mount('#app')
So following Vue Axios's docs, just insert the line for app.use()
:
import { createApp } from 'vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
const app = createApp(App)
app.use(VueAxios, axios) // π
app.mount('#app')
You could also chain it like this:
createApp(App).use(VueAxios, axios).mount('#app')
demo
You could import only axios
and define it as a global property :
Using a bundler (vue cli, vite or webpack ...):
import { createApp } from 'vue'
import axios from 'axios'
const app = createApp(...)
app.config.globalProperties.axios=axios
then use it in any child component like:
Option api :
this.axios.get(...)
in Composition api I recommend to import it directly like :
import axios from 'axios'
const MyComponent = {
setup() {
//use axios here
....
}
}
or you use useAxios
from the vueuse (vue composition utilities) :
import { useAxios } from '@vueuse/integrations/useAxios'
...
setup() {
const { data, isFinished } = useAxios('/api/posts')
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With