handleUpdate(row) {
this.reset()
const id = row.id || this.ids
getApply(id).then(async response => {
this.form = response.data
for (var i = 0; i < this.form.wmsReplenishApplyDetailList.length; i++) {
getInfo(this.form.wmsReplenishApplyDetailList[i].materialId).then(res => {
console.log(i)
console.log(this.form.wmsReplenishApplyDetailList[i])
console.log(res.data)
this.form.wmsReplenishApplyDetailList[i] = { ...this.form.wmsReplenishApplyDetailList[i], ...res.data }
})
}
console.log(this.form)
this.open = true
this.title = '修改补货申请'
})
},
这里的的i是var会出现闭包特性,由于JavaScript的事件循环机制,当异步操作完成并回调时,循环已经执行完成,i
的值已经变为循环结束时的值。在这种情况下,在getInfo
回调函数中,console.log(i)
输出的是循环结束时的值,而不是进入循环时的值。
这里将var更改为let即可解决