axios未在vue js cli中定义

Beg*_*ner 12 javascript vue.js axios vuejs2

我使用npm install axios命令安装了axios,这是我的package.json依赖项

 "dependencies": {
    "axios": "^0.18.0",
    "bootstrap-vue": "^2.0.0-rc.11",
    "vue": "^2.5.2",
    "vue-router": "^3.0.1"
  },
Run Code Online (Sandbox Code Playgroud)

我在我的main.js文件中注册了axios .

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)
Run Code Online (Sandbox Code Playgroud)

当我尝试在我的一个组件中使用axios时,我收到此错误:

Uncaught ReferenceError: axios is not defined
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题?

use*_*238 25

Vue.use意味着添加插件.但是,axios它不是一个插件Vue,因此您无法通过全局添加它use.

我的建议是axios仅在您需要时导入.但是,如果您确实需要全局访问它,您可能希望将其添加到原型中.

Vue.prototype.$axios = axios
Run Code Online (Sandbox Code Playgroud)

然后你可以使用axiosvue 访问this.$axios

  • 它说 vue 未定义 (2认同)

Aja*_*jay 10

使用以下命令安装 axios

 npm install axios --save
Run Code Online (Sandbox Code Playgroud)

执行上述命令导入后,如下所述:

import axios from 'axios'
Run Code Online (Sandbox Code Playgroud)


San*_*jay 9

Solution 1 (Not recommended):

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

window.axios = require('axios');
Run Code Online (Sandbox Code Playgroud)

And remove

Vue.use(axios)
Run Code Online (Sandbox Code Playgroud)

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
    }
}
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

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'
Run Code Online (Sandbox Code Playgroud)

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]
})
Run Code Online (Sandbox Code Playgroud)

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

That's it!


use*_*528 6

还安装vue-axios并导入main.js

import VueAxios from 'vue-axios'
Run Code Online (Sandbox Code Playgroud)

然后在main.js

Vue.use(VueAxios, axios)
Run Code Online (Sandbox Code Playgroud)

现在,如果我在您的方法中没有弄错,您可以使用例如:

let uri = 'http://localhost:4000/tickets/add';
this.axios.post(uri, this.ticket).then((response) => {
    console.log(response);
});
Run Code Online (Sandbox Code Playgroud)

  • `npm install vue-axios --save` (5认同)
  • 我需要从 npm 安装那个 VueAxios 吗? (2认同)

Jul*_*yag 5

import Vue from 'vue'
import axios from 'axios'

Vue.prototype.$http = axios;
Run Code Online (Sandbox Code Playgroud)

然后在您的组件中,您可以开始使用 axios,如下所示:

{
    methods: {
        someMethod() {
            this.$http.get('/users').then(() => {
                // do something
            })
        }
    }
}
Run Code Online (Sandbox Code Playgroud)