我有一个奇怪的 vuejs 效果,当我添加新的对象数据时,v-for 会重新渲染所有对象,即使它已经渲染了。
我正在实现像 face book 一样的无限滚动。
为了解释这段代码,我从 firebase 获取新数据,然后当数据到达屏幕底部时将其推送到数据对象中
var vueApp = new Vue({
el: '#root',
data: {
postList: [],
isOkaytoLoad: false,
isRoomPostEmpty: false,
},
mounted: function() {
// Everytime user scroll, call handleScroll() method
window.addEventLis tener('scroll', this.handleScroll);
},
methods: {
handleScroll: function()
{
var d = document.documentElement;
var offset = d.scrollTop + window.innerHeight;
var height = d.offsetHeight - 200;
// If the user is near the bottom and it's okay to load new data, get new data from firebase
if (this.isOkaytoLoad && offset >= height)
{
this.isOkaytoLoad = false;
(async()=>{
const lastPost = this.postList[this.postList.length - 1];
const room_id = PARAMETERS.get('room');
const q = query(collection(db, 'posts'), where('room', '==', room_id), orderBy("time", "desc"), limit(5), startAfter(lastPost));
const roomSnapshot = await getDocs(q);
roomSnapshot.forEach((doc) => {
const postID = doc.id;
(async()=>{
// Put the new data at the postList object
this.postList = [...this.postList, doc];
const q = query(collection(db, 'comments'), where('post_id', '==', postID));
const commentSnapshot = await getDocs(q);
doc['commentCount'] = commentSnapshot.size;
//this.postList.push(doc);
console.log(this.postList);
setTimeout(()=>{ this.isOkaytoLoad = true }, 1000);
})();
});
})();
}
}
}
})
看看这个屏幕记录,聚焦鼠标,它很滞后,当 vuejs 添加和加载新数据时我什至无法单击这些按钮
这是我使用的代码
我怀疑每次添加新数据时,VueJS 都会重新渲染所有数据,从而产生这种效果。如何强制 vueJS 不重新渲染那些已经在屏幕上渲染的数据?
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号