微信小程序方法中无法获取全局变量的数据怎么解决?
发布于 6 年前 作者 tiantao 4724 次浏览 来自 问答

我在全局变量中定义了一个strcount

getdata: function () {
    var that = this;  
    wx.request({
      url: 'https://website.defond.com.cn/WeChat_zjh/HappyFarm_WEChat.aspx',
      data: {
       
      },
      header: {
        "Content-Type": "applciation/json"
      },
      method: "GET",
      success: function (res) {
        console.log(res.data);
        var list = that.formatData(res.data.data),
          reccount = res.data.recordcount;
        that.strcount=reccount;
        console.log(list,reccount);
 
        that.setData({
          logs: res.data.data,
          strcount:reccount
          
 
        })
 
      },
      fail: function (err) { console.log(showRequestInfo()) },
      complete: function () { }
    })
  },

然后通过getdata把数据长度赋值给strcount  

createSimulationData: function () {
  var categories = [];
  var data = [];
  var that = this;
 
  for (var i = 0; i < 8; i++) {
    categories.push('2017-' + that.month + '-' + (that.day + (i + 1) - 9));
    data.push(that.temp + Math.random());
  }
 
  return {
    categories: categories,
    data: data
  }
},

但是在此方法中。我获取不到strcount  的值。

请问我该怎么做

2 回复

可以把代码发给你帮我看看吗?

没看出来你这第二段代码跟第一段代码是什么关系,也看不出第二段代码跟 strcount 是什么关系。

在你的第一段代码执行完之后,reccount 的值应该保存到了两个地方,一个是 page.strcount,一个是 page.data.strcount,怎么会获取不到呢?

回到顶部