异步加载问题
问题:
之前使用forEach循环加载的时候,console.log打印tagList的时候,显示articleId,但是当点击跳转的时候,console.log打印获取的id为undefined,item对象中也没有articleId,因为forEach不等待异步,而console.log会延迟显示,所以打印结果有,但是点击获取不到
<view class="btn" data-item="{{item}}" data-id="{{item.articleId}}" data-url='/news/newsDetails/index' wx:for="{{tagList}}" wx:key="id" bindtap="toDetails">
{{item.tagTitle}}
</view>
toDetails(e) {
const {
item,
id,
url,
} = e.currentTarget.dataset
console.log('id', id)
console.log('item', item)
wx.navigateTo({
url: url + '?id=' + (id || '') + '&title=' + this.data.alias,
})
},
async findTagList() {
let res = await findTagAndDescendantByAliasGyms({
tagType: 'article_classify',
tagAlias: 'xcx_hair',
})
let subList = res.data.subList || []
//subList.forEach(async item => {
//item.articleId = await this.findArticleList(item.id)
//})
for (let item of subList) {
item.articleId = await this.findArticleList(item.id)
}
this.setData({ tagList: subList })
},
async findArticleList(tagId) {
return new Promise(async (resolve, reject) => {
let res = await findPagingArticle({
tagId,
pageNum: 1,
pageSize: 10,
})
this.setData({
articleId: res.data.content[0]?.id || ''
})
resolve(res.data.content[0]?.id || '')
})
},
浙公网安备 33010602011771号