33

I installed axios using the npm install axios command this is my package.json dependencies

 "dependencies": {
    "axios": "^0.18.0",
    "bootstrap-vue": "^2.0.0-rc.11",
    "vue": "^2.5.2",
    "vue-router": "^3.0.1"
  },

I registered the axios in my main.js file.

import Vue from 'vue'
import VueRouter from 'vue-router'
import BootstrapVue from 'bootstrap-vue'

import axios from 'axios'
import App from './App'
import routerList from './routes'

Vue.use(axios)
Vue.use(BootstrapVue)
Vue.use(VueRouter)

When I tried to use axios in one of my components I get this error:

Uncaught ReferenceError: axios is not defined

How to fix this?

Andrey Popov
  • 7,362
  • 4
  • 38
  • 58
Beginner
  • 1,700
  • 4
  • 22
  • 42
  • 1
    The error might be from any file. Check all places you are trying to use it and make sure you import it properly everywhere! – Andrey Popov Jul 17 '18 at 06:16

8 Answers8

38

Vue.use means adding plugins. However, axios is not a plugin for Vue, so you can not add it globally via use.

My recommendation is importing axios only when you need it. But if you really need to access it globally, you may like to add it to prototype.

Vue.prototype.$axios = axios

Then you can access axios in vue using this.$axios

user3003238
  • 1,517
  • 10
  • 17
27

Solution 1 (Not recommended):

In main.js, add this line instead of import axios from 'axios'

window.axios = require('axios');

And remove

Vue.use(axios)

Solution 2 (Recommended Approach):

Using window is generally considered a bad practice, so you better use axios the following way:

  1. Create a folder named plugins inside of your src directory.

  2. Then, create axios.js file inside that directory. We will put all our axios logic here and use axios as a Vue plugin.

  3. Add the following:

import ax from 'axios'

// insert all your axios logic here

export const axios = ax

export default {
    install (Vue, options) {
        Vue.prototype.$axios = ax
    }
}
  1. In src/main.js, add the following:
import Vue from 'vue' // You can skip this line
import VueAxios from './plugins/axios'

Vue.use(VueAxios)

Now, you can use axios as this.$axios in your components. So something like this.$axios.get().

Therefore, you can import axios with the following line:

import { axios } from '@/plugins/axios'

Now, you can use axios directly in your store.

Or you can also use it as Vuex plugin:

import { axios } from '@/plugins/axios'

const axiosPlugin = store => {
   store.$axios = axios
}

new Vuex.Store({
    ...,
    plugins: [axiosPlugin]
})

Now, you can use it as this.$axios in Vuex.

Aryan Beezadhur
  • 4,503
  • 4
  • 21
  • 42
Axel
  • 4,365
  • 11
  • 63
  • 122
  • Don't know why it is getting downvotes. It does work! I just tried. – Axel Jul 17 '18 at 06:26
  • It does work, however, it is **always** a bad practice to assign variable to `window` unless no other choices are available. BTW, I am not the one who give you downvotes – user3003238 Jul 17 '18 at 06:31
  • @user3003238 Yes I know! Since he is not accepting any answers, I just gave him this solution. – Axel Jul 17 '18 at 06:32
  • Your answer works only for vue2. For vue3, you have to use ``createApp`` and ``app.use(VueAxios)``. – ikreb Jul 20 '21 at 14:54
13

Use following command to install axios

 npm install axios --save

After executing above command import like mentioned below:

import axios from 'axios'
Ajay
  • 917
  • 3
  • 12
  • 26
6

Also install vue-axios and import in main.js

import VueAxios from 'vue-axios'

Then in main.js:

Vue.use(VueAxios, axios)

Now if I am not mistaken in your methods you can use for example:

let uri = 'http://localhost:4000/tickets/add';
this.axios.post(uri, this.ticket).then((response) => {
    console.log(response);
});
Keale
  • 3,924
  • 3
  • 29
  • 46
user3918528
  • 145
  • 1
  • 1
  • 11
4
import Vue from 'vue'
import axios from 'axios'

Vue.prototype.$http = axios;

then inside your component you can start using axios like this:

{
    methods: {
        someMethod() {
            this.$http.get('/users').then(() => {
                // do something
            })
        }
    }
}
Julian Paolo Dayag
  • 3,562
  • 3
  • 20
  • 32
1

Include this line:

import {AxiosInstance as axios} from "axios";
Hamza Khan
  • 341
  • 2
  • 3
-1

To use in Vue Components, install both Vue Axios and Axios packages

npm install --save axios vue-axios

In your main.js file, add the following:

import Vue from 'vue'
import axios from 'axios'
import VueAxios from 'vue-axios'
 
Vue.use(VueAxios, axios)

With the above solution, I never had an issue using axios in my Vue components with this keyword till now.

Custom Javascript File

However, I had faced issues using Axios in a custom JS file with axios not defined error.

Following worked for me in a custom JS file:

const axios = require("axios");

Usage example:

export default {
  fetchProducts() {
    return axios.get(`${ROOT_URL}/products`);
  },
};
Rupak
  • 17
  • 1
  • 1
  • 5
  • 1
    This was usefeul for me thanks! I was working on a Custom JS and always get the error axios undefined but with this declaration const axios = require('axios') inside my Custom JS all the methods now worked just fine. BTW: I didn't install vue-axios, just using my axios globally installation was fine. – yehanny Apr 25 '21 at 18:14
  • @yehanny I am glad to hear it worked for you. Hope this answer and your comment helps someone else get a solution as well. And yes, according to the app setup one may have using Axios global installation might be sufficient. +1 – Rupak Apr 27 '21 at 07:46
-4

Try this codes:

import axios from 'axios'
    Vue.use(axios)
Nur Uddin
  • 1,798
  • 1
  • 28
  • 38