用setData给data中的数组传值不了?

发布于 6 年前作者 jingyi11641 次浏览最后编辑 6 年前来自 ask

Page({

data: {

arrday1: [],

},

onLoad: function (e) {

……

Product2.setQuery(andQuery2).find().then(res => {

this.setData({ arrday1: res.data.objects})

console.log("测试数据", res.data.objects)

console.log("测试数据", arrday1)

}, err => { })

}

res.data.objects里面数据是正常的,arrday1里没有数据是怎么回事?

6 回复
gang82
gang821 楼6 年前

onLoad下写 var that = this; 然后再 that.setData({arrday1:res.data.objects})

yan94
yan942 楼6 年前

this的指向对吗?比如换成 var that = this; 箭头函数里面的this用that代替 或者尝试着把最外面的数组改成对象接收(空字符接收也可以尝试下)

rguo
rguo3 楼6 年前

setData函数是异步的呀

juan27
juan274 楼6 年前

这样写,另外需要注意的是setData是异步的

WXOPEN Club 内容图片
eluo
eluo5 楼6 年前

大哥解决没有一样的问题

yanwang
yanwang6 楼4 年前

console.log(“测试数据”, this.data.arrday1)