axios 未在 vue js cli 中定义 [英] axios is not defined in vue js cli

查看:40
本文介绍了axios 未在 vue js cli 中定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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"
  },

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

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)

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

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

Uncaught ReferenceError: axios is not defined

如何解决这个问题?

推荐答案

Vue.use 表示添加插件.但是axios不是Vue的插件,所以不能通过use全局添加.

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

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

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

然后就可以在vue中使用this.$axios

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

这篇关于axios 未在 vue js cli 中定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆