【问题标题】:How to upgrade angular material MatPaginator to version 8如何将角材料 MatPaginator 升级到版本 8
【发布时间】: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 })上交的。所以我希望这能回答你的问题?

标签: angular angular-material


【解决方案1】:

首先,让我们记住,在将 Angular 及其库从直接的 npm 操作更新为使用 ng update 示意图时,我们需要更改范例。

如果您没有正确完成更新并且您没有版本控制来撤消更改并重试,您可以执行:

ng update @angular/core --migrate-only --from 7 --to 8

这将在您的项目中重新执行更新原理图。我不得不更新@angular/material,因为我有太多文件要更改,脚本内存不足:P

愉快的 ng 编码!

【讨论】:

    【解决方案2】:

    修复

    @ViewChild(MatPaginator , {static: true}) paginator: MatPaginator;
    
    @ViewChild(MatSort, {static: true}) sort: MatSort;
    

    【讨论】:

    • 不鼓励仅使用代码回答。请添加一些解释,说明这是如何解决问题的,或者这与现有答案有何不同。 From Review
    【解决方案3】:

    正如 Luiz 所建议的,添加 {static: true} 作为额外参数解决了这个问题。像这样设置它可以确保找到依赖于绑定解析的查询匹配(如结构指令*ngIf, etc...)。

    { static: false } 只能在 ngAfterViewInit 中访问。当您的模板上有结构指令(*ngIf 等)时,这也是您想要实现的目标。

    @ViewChild(MatPaginator , {static: true}) paginator: MatPaginator;
    
    @ViewChild(MatSort, {static: true}) sort: MatSort;
    

    【讨论】:

      猜你喜欢
      • 2019-05-23
      • 2019-11-24
      • 1970-01-01
      • 2018-12-26
      • 2018-08-12
      • 1970-01-01
      • 2019-01-10
      • 1970-01-01
      • 2020-12-09
      相关资源
      最近更新 更多