我在我的Angular项目中使用mat-autocomplete https://material.angular.io/components/autocomplete/api,但是当加载大量数据(25k条)时,性能会变差。搜索和自动完成建议的速度太慢了。如何提高这个组件的性能?
我在我的Angular项目中使用mat-autocomplete https://material.angular.io/components/autocomplete/api,但是当加载大量数据(25k条)时,性能会变差。搜索和自动完成建议的速度太慢了。如何提高这个组件的性能?
我遇到了相同的情况,我的解决方案是将列表限制为前N个结果。所以代码看起来像这样:
component.html
<mat-autocomplete #auto="matAutocomplete" [displayWith]="name">
<mat-option *ngFor="let item of filteredItems" [value]="item">
{{item.name}}
</mat-option>
</mat-autocomplete>
组件.ts
this.filteredItems = this.items.filter(...some.filtering here...).slice(0, 10);
您将过滤所有项目,但仅显示前10个匹配项。;-)
如果自动完成中的数据很多,性能会受到影响,我使用*cdkVirtualFor取代了自动完成功能中的*ngFor来解决这个问题。
请参考以下引用。