【发布时间】:2020-02-27 06:04:53
【问题描述】:
我正在制作一个 Angular 组件,该组件将用于根据输入字符串显示文件图标。将根据每个文件类型的一些可能值的数组检查输入字符串。
例如,
- 如果我们看到 doc、docx、word,则显示 word 图标。
- 如果我们看到 xls、xlsx、excel,则显示 excel 图标。
等等,对于我们的应用程序期望的每种文件类型。
我正计划根据输入字符串所在的数组创建一个带有适当类的 <i> 元素的简单列表。
export class FileIconComponent {
@Input() fileType: string;
private readonly FILE_WRD: string[] = ["word", "doc", "docx"];
private readonly FILE_PDF: string[] = ["pdf", "application/pdf"];
private readonly FILE_EXL: string[] = ["excel", "xls", "xlsx"];
constructor() {}
}
<i *ngIf="FILE_EXL.includes(fileType?.toLowerCase())" class="icon document-excel-o"></i>
<i *ngIf="FILE_WRD.includes(fileType?.toLowerCase())" class="icon document-word-o"></i>
<i *ngIf="FILE_PPT.includes(fileType?.toLowerCase())" class="icon document-powerpoint-o"></i>
但是,由于更改检测每秒运行很多次,并且我可能每页显示一百行,所以当我在模板中放置函数调用时,我担心性能。制作一个 <i> 并使用 ngClass 来确定类会更高效吗?
有没有更有效的方法来做到这一点?还是我在这种情况下过度关注性能?
【问题讨论】:
标签: angular