小程序Cannot read property 'setData' of null;

错误描述:
Cannot read property 'setData' of null;at pages/ranking-reward/index onLoad function;at api request success callback function
TypeError: Cannot read property 'setData' of null
源码:
onLoad: function() {
    this.getRewardData(this.page);
  },

  getRewardData: function(page) {
    // var that = this;
    wx.request({
      url: app.apiUrl + '/Sysj224/RewardVideo/ranking?member_id=2373726&type=sendGiftRanking',
      data: {
        page: page
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: function(res) {
        // this.data.rankData = res;
        this.setData({    #这里报错了
          rankData: res
        })
        console.log(res);

      }
    })
  },
错误原因:

因为this作用域指向问题,success方法指向闭包,所以这个this是闭包里面。

正确的做法如下:

小程序Cannot read property 'setData' of null;_第1张图片

你可能感兴趣的:(小程序)