VUE簡單解決控制檯打印數組爲空,但是展開有數據

如圖,在控制打印的時候數組是空,但是展開可以看到有37條數據。緊接着打印了長度,長度爲0.在這裏插入圖片描述
這個問題我搜了很多解決都是

this.$nextTick(() => {

  })

我有個更簡單的辦法,這個算是異步的一個加載
那就使用延時setTimeout來觸發

let rightItems = this.$refs.r_list.getElementsByClassName('proList')
 setTimeout(() => { // 根據betterScroll定義滾動
   console.log(rightItems) // 右側列表數組內容
   console.log(rightItems.length) // 右側列表數組長度
   if (rightItems && (rightItems.length > 0)) {
     let height = 0
     this.arr.push(height)
     for (let i = 0; i < rightItems.length; i++) {
       let item = rightItems[i]
       height += item.clientHeight
       this.arr.push(height)
     }
   }
 }, 600)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章