NH
Size: a a a
NH
IS
IK
ngIf
vs ngSwitch
. (mat-table и все чайлды в onPush)mat-table
, в которой в td
лежат поля разных типов. ну например, текст, имедж, кнопка copy to clipboard компонент, булеан поле... такое.enum TYPES
который залетал в каждом поле в <ng-container *ngIf="col.type === TYPES.image">...
все бы ничего, но полей стало больше, и пагинатор юзается все время в 200 айтемов. тормозит :) это как бы ишью известный у mat-table о slow rendering over 100 items, но в любом случае, начать с чего то надо - пришло время рефакторить ngIf в ngSwitch. Вроде как логично, свитч должен быть быстрее. но НЕТ! :) на глаз заметно что дольше, и я не могу обьяснить почему. ng-template
, и оттуда чекать перформанс меняя пачку ngSwitch полей и ngIf полей просто заменой #рефы нужной ng-template
. и тут еще странность - в темплейте скорость рендеринга полей упала раза в три! D
NH
IK
ngIf
внутри td
<table mat-table matSort multiTemplateDataRows ... >
<ng-container *ngFor="let col of columns" [matColumnDef]="col.slug">
<!-- CUSTOM COLUMNS -->
<!-- THEAD OF CUSTOM COLUMNS -->
<th mat-sort-header mat-header-cell *matHeaderCellDef>
<span [innerHTML]="col.slug"></span>
</th>
<!-- TD -->
<td mat-cell *matCellDef="let element">
<!-- START OF CUSTOM CELLS -->
<!-- if link is computed -->
<ng-container *ngIf="col.type === TYPES.link && col.link">
<a [routerLink]="col | somePipeGenerateLink: element">
{{ element[col.slug] }}
</a>
</ng-container>
<!-- if column is simple text -->
<ng-container *ngIf="col.type === TYPES.text">
{{ element[col.slug] | somePipeOne | somePipeSecond: col. slug }}
</ng-container>
<!-- copy to clipboard button -->
<ng-container *ngIf="col.type === TYPES.copytoclipboard">
<app-button-clipboard [valueToCopy]="element[col.slug]"></app-button-clipboard>
</ng-container>
<!-- END OF CUSTOM CELLS -->
</td>
</ng-container>
</table>
IK
ng-template
полями ngIf
и ngSwitch
именованные в темплейтах как ngIfCells
и ngSwitchCells
<table mat-table matSort multiTemplateDataRows ... >
<ng-container *ngFor="let col of columns" [matColumnDef]="col.slug">
<!-- CUSTOM COLUMNS -->
<!-- THEAD OF CUSTOM COLUMNS -->
<th mat-sort-header mat-header-cell *matHeaderCellDef>
<span [innerHTML]="col.slug"></span>
</th>
<!-- TD -->
<td mat-cell *matCellDef="let element">
<ng-template
[ngTemplateOutlet]="ngIfCells"
[ngTemplateOutletContext]="{col: col, element: element}"
></ng-template>
</td>
</ng-container>
</table>
...
<ng-template #ngIfCells let-col="col" let-element="element">
<!-- START OF CUSTOM CELLS -->
<!-- if link is computed -->
<ng-container *ngIf="col.type === TYPES.link && col.link">
<a [routerLink]="col | somePipeGenerateLink: element">
{{ element[col.slug] }}
</a>
</ng-container>
<!-- if column is simple text -->
<ng-container *ngIf="col.type === TYPES.text">
{{ element[col.slug] | somePipeOne | somePipeSecond: col. slug }}
</ng-container>
<!-- copy to clipboard button -->
<ng-container *ngIf="col.type === TYPES.copytoclipboard">
<app-button-clipboard [valueToCopy]="element[col.slug]"></app-button-clipboard>
</ng-container>
<!-- END OF CUSTOM CELLS -->
</ng-template>
<ng-template #ngSwitchCells let-col="col" let-element="element">
<!-- START OF CUSTOM CELLS -->
<ng-container [ngSwitch]="col.type">
<!-- if link is computed -->
<ng-container *ngSwitchCase="TYPES.link">
<a [routerLink]="col | somePipeGenerateLink: element">
{{ element[col.slug] }}
</a>
</ng-container>
<!-- if column is simple text -->
<ng-container *ngSwitchCase="TYPES.text">
{{ element[col.slug] | somePipeOne | somePipeSecond: col. slug }}
</ng-container>
<!-- copy to clipboard button -->
<ng-container *ngSwitchCase="TYPES.copytoclipboard">
<app-button-clipboard [valueToCopy]="element[col.slug]"></app-button-clipboard>
</ng-container>
<!-- END OF CUSTOM CELLS -->
</ng-container>
</ng-template>
IK
IK
V
E
ОС
E
IK
NH
IK
ngOnChanges
. D
IK
D
M