vue中解决出现[Vue warn]: Error in created hook: "ReferenceError: axios is not defined"问题

正确解决vue中axios is not defined

axios已经是安装好的状态

在学习vue时,前段请求后端数据时浏览器出现下面提示的错误

在main.js下

import VueAxios from 'vue-axios';
import axios from 'axios';

使用的npm 例子的代码如下:

created(){
    const  _this = this
    axios.get('http://localhost:8081/book/findall').then(function (resp) {
        _this.books = resp.data;
      })
  }
}
请求时浏览器出现以下错误
webpack-internal:///./node_modules/vue/dist/vue.esm.js:629 [Vue warn]: Error in created hook: "ReferenceError: axios is not defined"
found in
--->  at src/components/Book.vue
    at src/App.vue
     
     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;
      })
  }
}
改好之后 运行 问题得到解决

你可能感兴趣的:(vue)