项目需要是进入微信小程序,在onLoad
函数中使用wx.request
向服务器发起请求,然后将请求通过this.setData
保存到一个data:{items:[]}
属性中,在页面中使用<block wx:for="{{itmes}}">{{item.name}}</block>
却没有数据,这是为什么呢?
// index.wxml
<block wx:for="{{itmes}}">
<view class='item'>
<text>{{item.name}}</text>
<text>{{item.desc}}</text>
</view>
</block>
// index.js
data:{
items:[]
},
onLoad: function () {
var that = this;
wx.request({
url: 'http://localhost:8888/test.php',
success: (res) => {
console.log(res)
// 可以正确输出数组数据
that.setData({
items: res
})
console.log(that.data.activity)
// 也可以正确输出数据
}
})
实在是没有想通是怎么回事,我以为是异步的问题,所以使用点击按钮发起请求的方式试验了一次,结果相同.
你把数据存在data.activity中,渲染页面的是data.items 怎么会有数据呢?