created: function () {
      this.$axios.post('/jsonData').then( function (res) {
        this.cares = res.data;
        console.log(this.cares)
      })
 
 以上报错‘undefined’
 
经过查询得知,.then回调里的this指向的不是vue实例,所以出错。
 
解决办法:
  1、修改this指向,原生js可以用.bind()方法
  2、ES6 箭头函数
    
  .then(  res => {
        this.cares = res.data;
        console.log(this.cares)
      })
 

转载于:https://www.cnblogs.com/yinblog/p/11549916.html

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐