鍍金池/ 問答/Linux  HTML/ vue 中在computed 里面使用 forEach 怎么總是報錯說forEa

vue 中在computed 里面使用 forEach 怎么總是報錯說forEach 沒定義

vue 中在computed 里面使用 forEach 怎么總是報錯說forEach 沒定義

clipboard.png

clipboard.png
確定前面是個數(shù)組,并且第一次頁面都出來了,然后刷新 就報錯了 這是怎么回事呢

clipboard.png

回答
編輯回答
命多硬

打個斷點,看看你的videoGame變成啥了。

2018年7月22日 00:15
編輯回答
裸橙

更新:

你在 then 方法里 this 就不是指向當(dāng)前 vue 實例了,你需要在外部保存一個引用:

const self = this
apiCall
    .then(data => {
        // filter data
        self.videoGames.push(data)
    })

它說的不是 forEach 未定義,說的是 videoGame[0] 未定義,你在遍歷之前把 videoGame[0] 打出來看一下。

2017年12月19日 16:18