axios is not defined in vue js cli

2019-04-09 08:55发布

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?

6条回答
男人必须洒脱
2楼-- · 2019-04-09 09:33
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
            })
        }
    }
}
查看更多
Melony?
3楼-- · 2019-04-09 09:35

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 (it's funny that Laravel actually uses this way), 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 axios_http from 'axios'

// insert all your axios logic here

export const axios = axios_http

export default {
    install (Vue, options) {
        Vue.prototype.$axios = axios_http
    }
}

4) 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().

However, in Vuex, you cannot access this so you have got to import axios from plugins/axios.js file.

Therefore, you can import axios with the following line:

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

Now, you can use axios directly in your store.

That's it!

查看更多
我想做一个坏孩纸
4楼-- · 2019-04-09 09:41

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

查看更多
成全新的幸福
5楼-- · 2019-04-09 09:50

Try this codes:

import axios from 'axios'
    Vue.use(axios)
查看更多
混吃等死
6楼-- · 2019-04-09 09:52

i found in laravel project window.axios = require('axios'); when i insert it in myproject. that all be fine!

查看更多
做个烂人
7楼-- · 2019-04-09 10:00

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);
});
查看更多
登录 后发表回答