axios未在vue js cli中定义 [英] axios is not defined in 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
的插件,因此您无法通过全局添加它使用
。
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中访问 axios
使用这个。$ axios
Then you can access axios
in vue using this.$axios
这篇关于axios未在vue js cli中定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!