vue2.0使用axios获取不到数据

main.js

import Axios from 'axios'

Vue.prototype.$http = Axios

index.vue

created: function () {
    this.$http.get('http://localhost:8080/static/db.json')
    .then(res => {
      this.productList = res.data.productList
      this.newsList = res.data.getNewsList
    }, err => {
      console.log(err)
    })
}

为什么获取不到数据呢?也没有报错...

阅读 5.7k
3 个回答
created: function () {
    this.$http.get('http://localhost:8080/static/db.json').then(res => {
      this.productList = res.data.productList
      this.newsList = res.data.getNewsList
    }).catch(err=>{
        console.log(err)
    })
}

试试看,不确定是不是这里的问题,要this.$http.get().then().catch()这种写法吧

解决了,不小心把Vue.use(VueRouter)给删了,所以页面不显示,我的天。。。

then()方法里面的this指向已经变了,试试下面的把this先暂存起来,再在then()方法里面使用。
created: function () {

var _this = this;
this.$http.get('http://localhost:8080/static/db.json')
.then(res => {
  _this.productList = res.data.productList
  _this.newsList = res.data.getNewsList
}).catch(err=>{
    console.log(err)
})

}

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题