简体   繁体   English

Angular2:使用较大的ngFor List增强滚动事件的性能

[英]Angular2: Enhance the performance of scroll event with a big ngFor List

I'm developing a web chat, the structute of the chat shows the messages like this: 我正在开发网络聊天,聊天结构显示如下消息:

<div class="scrollable" (scroll)="detectTop($event)">
    <div class="message-ballon" *ngfor="message in messages">
        <span class="time-chat" *ngIf="showTime(message)"> {{message.time}} </span>
        <p class="message-content"> {{message.content}} </p>
     </div>
</div>

My detectTop(e) function checks if scrollTop === 0 if yes it get more messages from server and updates messages with it. 我的detectTop(e)函数检查是否scrollTop === 0,如果是,它将从服务器获取更多消息并使用它更新消息。

My showTime function compares the message with the messages above it to check if it is the same date. 我的showTime函数将消息与其上方的消息进行比较,以检查它是否为同一日期。

The problem is the app is painfully slow, without the scroll event the scrolls works ok and is very fast. 问题在于该应用程序运行缓慢,没有滚动事件,滚动正常且非常快。

I really want to increase the performance, but I'm not figuring how. 我确实想提高性能,但是我不知道如何做。

There is a faster way to detect scrollTop event and not impact so much on performance? 有一种更快的方法来检测scrollTop事件,而不会对性能产生太大影响?

This problem is big even with small 10 messages list, and with more than 100 messsages the scroll stops 即使只有10条消息列表,问题也很大,并且有超过100条消息,滚动停止

try adding a tracker to the *ngFor loop: 尝试将跟踪器添加到* ngFor循环中:

*ngFor="let message of messages; trackBy:messageTracker

and in your JS code: 并在您的JS代码中:

messageTracker(index, message) {
  return message? message.id : undefined;
}

this will avoid re-rendering the messages on scroll 这样可以避免重新渲染滚动消息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM