简体   繁体   English

axios 未在 vue js cli 中定义

[英]axios is not defined in vue js cli

I installed axios using the npm install axios command this is my package.json dependencies我使用npm install axios命令npm install axios这是我的package.json依赖项

 "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.我在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)

When I tried to use axios in one of my components I get this error:当我尝试在我的一个组件中使用 axios 时,出现此错误:

Uncaught ReferenceError: axios is not defined

How to fix this?如何解决这个问题?

Vue.use means adding plugins. Vue.use意味着添加插件。 However, axios is not a plugin for Vue , so you can not add it globally via use .但是, axios不是Vue的插件,因此您无法通过use全局添加它。

My recommendation is importing axios only when you need it.我的建议是仅在需要时才导入axios 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然后,您可以访问axios在VUE利用this.$axios

Solution 1 (Not recommended):解决方案1(不推荐):

In main.js , add this line instead of import axios from 'axios'main.js ,添加这一行而不是import axios from 'axios'

window.axios = require('axios');

And remove并删除

Vue.use(axios)

Solution 2 (Recommended Approach):解决方案 2(推荐方法):

Using window is generally considered a bad practice, so you better use axios the following way:使用window通常被认为是一种不好的做法,因此您最好按以下方式使用axios

  1. Create a folder named plugins inside of your src directory.src目录中创建一个名为plugins的文件夹。

  2. Then, create axios.js file inside that directory.然后,在该目录中创建axios.js文件。 We will put all our axios logic here and use axios as a Vue plugin.我们将把所有的 axios 逻辑放在这里,并使用 axios 作为 Vue 插件。

  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:src/main.js ,添加以下内容:
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.现在,您可以在组件中使用 axios 作为this.$axios So something like this.$axios.get() .所以类似this.$axios.get()

Therefore, you can import axios with the following line:因此,您可以使用以下行导入 axios:

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

Now, you can use axios directly in your store.现在,您可以直接在您的商店中使用axios

Or you can also use it as Vuex plugin:或者您也可以将其用作 Vuex 插件:

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.现在,您可以在 Vuex 中将其用作this.$axios axios。

Use following command to install axios使用以下命令安装 axios

 npm install axios --save

After executing above command import like mentioned below:执行上述命令导入后,如下所述:

import axios from 'axios'

Also install vue-axios and import in main.js还要安装vue-axios并在main.js导入

import VueAxios from 'vue-axios'

Then in main.js :然后在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);
});
import Vue from 'vue'
import axios from 'axios'

Vue.prototype.$http = axios;

then inside your component you can start using axios like this:然后在您的组件中,您可以开始使用 axios,如下所示:

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

包括这一行:

import {AxiosInstance as axios} from "axios";

i found in laravel project window.axios = require('axios'); 我在laravel项目window.axios = require('axios'); when i insert it in myproject. 当我将其插入myproject中时。 that all be fine! 一切都很好!

To use in Vue Components , install both Vue Axios and Axios packages要在Vue Components 中使用,请安装Vue AxiosAxios

npm install --save axios vue-axios

In your main.js file, add the following:在 main.js 文件中,添加以下内容:

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.使用上述解决方案,到目前为止,我在 Vue 组件中使用this关键字使用axios 时从未遇到过问题。

Custom Javascript File自定义 JavaScript 文件

However, I had faced issues using Axios in a custom JS file with axios not defined error.但是,我在自定义 JS 文件中使用Axios时遇到了问题,但axios 未定义错误。

Following worked for me in a custom JS file:以下在自定义 JS 文件中为我工作:

const axios = require("axios");

Usage example:用法示例:

export default {
  fetchProducts() {
    return axios.get(`${ROOT_URL}/products`);
  },
};

Try this codes:试试这个代码:

import axios from 'axios'
    Vue.use(axios)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM