鍍金池/ 問(wèn)答/HTML/ vue中為什么[__ob__: Observer]下無(wú)法取到數(shù)據(jù)

vue中為什么[__ob__: Observer]下無(wú)法取到數(shù)據(jù)

vue中為什么[__ob__: Observer]下無(wú)法取到數(shù)據(jù),可是控制臺(tái)明明有數(shù)據(jù)
圖片描述

圖片描述

不知道是否因?yàn)閜romise的原因?

回答
編輯回答
敢試

問(wèn)題出在異步上,跟 [__ob__: Observer] 無(wú)關(guān)

原因在與,你同時(shí)發(fā)起了幾個(gè)請(qǐng)求,但是 jsonp() 調(diào)用結(jié)果返回的順序不確定,可能會(huì)導(dǎo)致 index=1then() 函數(shù)比 index=0then() 函數(shù)先執(zhí)行,此時(shí) this.detailId 數(shù)組只有一個(gè)值,而你取的時(shí)候是 this.detailId[1](因?yàn)?index=1的結(jié)果先返回),所以報(bào)錯(cuò)了

下面提供一種修復(fù)的方式,在 then() 函數(shù)里做點(diǎn)改動(dòng):

// remove
// this.detailId.push(new Movie(res.id, res.wish_count))

// add
this.$set(this.detailId, index, new Movie(res.id, res.wish_count))
2018年8月12日 21:51