Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly import Axios in vue 3 after creating new project with CLI?

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?

like image 434
RDU Avatar asked Oct 08 '20 19:10

RDU


People also ask

Is Axios compatible with Vue 3?

in Vue 3. This wrapper bind axios to app instance or this if you're using single file component.

What is VueAxios?

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.

How do I use Axios in Vue?

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

What are the components of Vue tutorialslist?

– 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.

How do I install Axios on VSCode?

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.

How to make Axios available globally in the browser?

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


Video Answer


2 Answers

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

like image 94
tony19 Avatar answered Oct 22 '22 09:10

tony19


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')
 }
like image 27
Boussadjra Brahim Avatar answered Oct 22 '22 09:10

Boussadjra Brahim