that.data取不到data中的值是为啥?
发布于 4 年前 作者 ping87 2196 次浏览 来自 官方Issues
data: {
    time:"",
    count:1
   },
   checkupfunction(){
     
     //打卡
     var TIME = util.formatTime(new Date());
     this.data.time = TIME
     //获取当前时间
     var that = this
     this.data.count = 1
     var stic = getApp()
     var getxuehao = stic.globalData.xuehao
     //传递学号
     console.log(this.data.count)
}

 data: {
    massage:"",
    time:"",
    count:1
  },
  getdata:function(e){
    this.setData({
      massage:e.detail.value
    });
  },
  setmassagefunction(){
    
    //打卡
    var TIME = util.formatTime(new Date());
    this.data.time = TIME
    //获取当前时间
    var that = this
    var stic = getApp()
    var getxuehao = stic.globalData.xuehao
    //传递学号
    wx.cloud.init()
    const db = wx.cloud.database()
    //连接数据库
    console.log(getxuehao)
    console.log(that.data.count)
}

上下连段代码,下面那个可以直接打印出count值,上面那段打印结果为“Undefined” ,使用“this.data.count = 1”才能正确读值,两端代码一模一样,只是写在不同页面,实在理解不了是为啥,有没有大佬可以帮忙解释一下。

1 回复
用setData
var that=this;
this.setData({
count:1,
res=>{
  console.log(that.data.count);
});
回到顶部