I installed axios using the npm install axios
command this is my package.json
dependencies
JavaScript
x
7
1
"dependencies": {
2
"axios": "^0.18.0",
3
"bootstrap-vue": "^2.0.0-rc.11",
4
"vue": "^2.5.2",
5
"vue-router": "^3.0.1"
6
},
7
I registered the axios in my main.js
file.
JavaScript
1
12
12
1
import Vue from 'vue'
2
import VueRouter from 'vue-router'
3
import BootstrapVue from 'bootstrap-vue'
4
5
import axios from 'axios'
6
import App from './App'
7
import routerList from './routes'
8
9
Vue.use(axios)
10
Vue.use(BootstrapVue)
11
Vue.use(VueRouter)
12
When I tried to use axios in one of my components I get this error:
JavaScript
1
2
1
Uncaught ReferenceError: axios is not defined
2
How to fix this?
Advertisement
Answer
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.
JavaScript
1
2
1
Vue.prototype.$axios = axios
2
Then you can access axios
in vue using this.$axios