小编7R1*_*R1X的帖子

在 Angular 组件模板中调用简单方法会导致性能问题吗?

通过阅读一些 Angular 资源(见下文),调用模板中的方法可能会导致性能问题。例如

<div>{{getDisplayName()}}</div>
Run Code Online (Sandbox Code Playgroud)

然而,相当多的 Angular Material 库示例调用模板中的方法。

树

https://material.angular.io/components/tree/examples

<mat-icon class="mat-icon-rtl-mirror">
    {{treeControl.isExpanded(node) ? 'expand_more' : 'chevron_right'}}
</mat-icon>
Run Code Online (Sandbox Code Playgroud)

带有选择的表

https://material.angular.io/components/table/examples

<td mat-cell *matCellDef="let row">
  <mat-checkbox (click)="$event.stopPropagation()"
                (change)="$event ? selection.toggle(row) : null"
                [checked]="selection.isSelected(row)"
                [aria-label]="checkboxLabel(row)">
  </mat-checkbox>
</td>
Run Code Online (Sandbox Code Playgroud)

这些方法确实从 Set 中返回一个值。例如 https://github.com/angular/components/blob/4d4ee182de3f306c5fcf49853ef6ac71c4000eea/src/cdk/collections/selection.ts

isSelected(value: T): boolean {
   return this._selection.has(value);
}
Run Code Online (Sandbox Code Playgroud)

这些方法会像*普通方法一样导致性能问题吗?或者我缺少什么?

资源

在模板中调用函数是否会导致 Angular2+ 中的性能问题?

https://blog.mgechev.com/2017/11/11/faster-angular-applications-onpush-change-detection-immutable-part-1/

javascript angular2-template angular

5
推荐指数
1
解决办法
620
查看次数

标签 统计

angular ×1

angular2-template ×1

javascript ×1