axios已经是安装好的状态
在学习vue时,前段请求后端数据时浏览器出现下面提示的错误
1.在main.js下
import VueAxios from 'vue-axios';
import axios from 'axios';
2.使用的npm 例子的代码如下
created(){
const _this = this
axios.get('http://localhost:8081/book/findall').then(function (resp) {
_this.books = resp.data;
})
}
}
3.请求时浏览器出现以下错误
webpack-internal:///./node_modules/vue/dist/vue.esm.js:629 [Vue warn]: Error in created hook: "ReferenceError: axios is not defined"
found in
---> <Book> at src/components/Book.vue
<App> at src/App.vue
<Root>
warn @ webpack-internal:///./node_modules/vue/dist/vue.esm.js:629
解决办法
在main.js下 改成
import VueAxios from 'vue-axios';
import axios from 'axios';
Vue.prototype.$axios = axios;
例子的代码改成:
created(){
const _this = this
this.$axios.get('http://localhost:8081/book/findall').then(function (resp) {
_this.books = resp.data;
})
}
}