【发布时间】:2019-06-03 15:15:55
【问题描述】:
我最近将我的项目(包括角度材料)升级到了版本 8。但是,有一些问题我不知道如何解决。
component.ts
@ViewChild(MatPaginator) paginator: MatPaginator;
@ViewChild(MatSort) sort: MatSort;
component.html
<mat-table #table [dataSource]="dataSource" matSort>
<!-- Class Column -->
<ng-container matColumnDef="class">
<mat-header-cell *matHeaderCellDef mat-sort-header> Class </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.class}} </mat-cell>
</ng-container>
<!-- Topic Column -->
<ng-container matColumnDef="topic">
<mat-header-cell *matHeaderCellDef mat-sort-header> Topic </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.topic}} </mat-cell>
</ng-container>
<!-- Tme Column -->
<ng-container matColumnDef="datetime">
<mat-header-cell *matHeaderCellDef mat-sort-header> Date & Time </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.time | date:'medium'}} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;" (click)="show(row)"></mat-row>
</mat-table>
<mat-paginator #paginator [pageSize]="5" [pageSizeOptions]="[5, 10, 20]" [showFirstLastButtons]="true">
</mat-paginator>
上面的 ViewChild 现在需要 2 个参数。 https://angular.io/guide/static-query-migration
我不确定如何更新这些,因为它们取自 angular material 版本 7 文档。
新版本的文档没有这些 ViewChilds。 https://material.angular.io/components/paginator/examples
我没有看到 angular material 8 迁移指南。
【问题讨论】:
-
您是否使用
ng update命令进行了更新?它在您的代码上运行原理图,以自动修复和迁移易于更改的代码。ViewChild绝对是要运行的自动升级之一。可以运行migration only版本:ng update @angular/core --from 7 --to 8 --migrate-only -
是的,我还读到它应该自动完成,我已经完成了 ng update 命令,它并没有为我改变这个,
-
嗯,我看到的所有自动更新的例子都是在
@ViewChild(MatExample, { static: false })上交的。所以我希望这能回答你的问题?