that.setData 没有生效问题 如下图
发布于 5 年前 作者 taojie 7039 次浏览 来自 问答

在setData里面的第二个没有生效!!  O(∩_∩)O谢谢

6 回复
that.setData({
  latelyPlayId: 1000
});
that.tap();

这一块有问题,setData 之后直接输出 latelyPlayId 好像不可以,按照之前 react 的经验 setData 应该是异步的,最好加一个 setTimeout 之后在输出看结果~

Page({
data: {
latelyPlayId: 45,
},

tap: function () {
const that = this;
console.info(that.latelyPlayId + " that.latelyPlayId");
},
onReady: function () {
const that = this;
wx.request({
url: '',
data: {_: new Date().getTime()},
header: {
'content-type': 'application/json'
           },
method: 'GET',
success: function (res) {
that.setData({
latelyPlayId: 1000
               });
that.tap();
},
})
}
});

Page.prototype.setData()

setData 函数用于将数据从逻辑层发送到视图层,同时改变对应的 this.data 的值。

注意:

  1. 直接修改 this.data 无效,无法改变页面的状态,还会造成数据不一致。

  2. 单次设置的数据不能超过1024kB,请尽量避免一次设置过多的数据

谢谢各位!! 问题已解决

@人生海海    setTimeout 之前试过,不行。

that.data.latelyPlayId = 1000; 这里做什么? 给page-> data -> latelyPlayId 赋值?

应该这么写:

var latelyPlayId_ = 1000;

that.setDate({

latelyPlayId: latelyPlayId_

})

提供的信息太少了,建议用以下方式排查错误:

  • 单独设置一项是否生效

  • 直接设置常量,例如 `setData({ latelyPlayId: 1000 })`,看是否生效

回到顶部