gpt4 book ai didi

angular - 制作 Material 分页器时无法读取未定义的属性 'page'

转载 作者:行者123 更新时间:2023-12-02 20:58:44 25 4
gpt4 key购买 nike

我一直在关注 Angular Material 数据表分页器教程 here .

当他们构建分页器时,他们使用声明一个分页器

@ViewChild(MatPaginator) 分页器:MatPaginator;

在他们的 component.ts 文件中。通过此声明,他们可以稍后访问同一文件中的属性:

 ngAfterViewInit() {
this.paginator.page
.pipe(
tap(() => this.loadLessonsPage())
)
.subscribe();
}

没有问题。

当我在组件中做同样的事情时:

export class AllMatchesComponent implements OnInit, OnDestroy, AfterViewInit {
...
@ViewChild(MatPaginator) paginator: MatPaginator;
...
ngAfterViewInit(){
console.log(this.paginator);
this.paginator.page
.pipe(
tap(()=> this.loadMatchesPage())
)
.subscribe();
}
...

,我在控制台中收到以下错误:

ERROR TypeError: Cannot read property 'page' of undefined at AllMatchesComponent.ngAfterViewInit (all-matches.component.ts:50)

当我尝试查看他们的示例 repo 时(转到分支 2-data-table-finished),代码包含所有过滤和排序语法,我在教程中尚未了解这些语法。换句话说,他们的示例代码与我在教程中执行的步骤不同,并且没有其他分支可以捕获更简单状态的代码。

我不知道为什么分页器未定义。有什么想法吗?

我的个人存储库可以在这里找到:

git clone https://github.com/Atticus29/dataJitsu.git
cd dataJitsu
git checkout SO-paginator-freeze

为了方便起见,这里是整个 all-matches.component.ts:

import { Component, OnInit, OnDestroy, AfterViewInit, ViewChild } from '@angular/core';
import { D3Service } from '../d3.service';
import { DatabaseService } from '../database.service';
import { TextTransformationService } from '../text-transformation.service';
import * as firebase from 'firebase/app';
import { MatTableDataSource, MatSort } from '@angular/material';
import { MatPaginatorModule } from '@angular/material/paginator';
import { DataSource } from '@angular/cdk/table';
import {MatProgressSpinnerModule} from '@angular/material/progress-spinner';
import { MatchDataSource } from '../matchDataSource.model';
import { AuthorizationService } from '../authorization.service';
import { Subject } from 'rxjs/Subject';
import { tap } from 'rxjs/operators';
import { MatPaginator } from '@angular/material';

@Component({
selector: 'app-all-matches',
templateUrl: './all-matches.component.html',
styleUrls: ['./all-matches.component.scss']
})
export class AllMatchesComponent implements OnInit, OnDestroy, AfterViewInit {
private dataSource: MatchDataSource;
private columnsToDisplay = ['rank','weightClass', 'ageClass','athlete1Name', 'athlete2Name', 'gender','tournamentName','location', 'date', 'matchRating', 'videoUrl']; //TODO make this dynamic somehow
private loading = true;
user: any = null;
private ngUnsubscribe: Subject<void> = new Subject<void>();
private matchCount: number;
private pageSize: number;

@ViewChild(MatPaginator) paginator: MatPaginator;

constructor(private authService: AuthorizationService, private d3Service: D3Service, private dbService: DatabaseService, private textTransformationService: TextTransformationService) { }

ngOnInit() {
this.authService.getCurrentUser().takeUntil(this.ngUnsubscribe).subscribe(user=>{
this.user = user;
},err=>{
console.log(err);
});
this.pageSize = 2;
this.dataSource = new MatchDataSource(this.dbService);
this.dataSource.loadMatches('test', '', '', 0, this.pageSize);
this.dbService.getMatchCount().subscribe(results=>{
this.matchCount = results;
});
}

ngAfterViewInit(){
console.log(this.paginator);
this.paginator.page
.pipe(
tap(()=> this.loadMatchesPage())
)
.subscribe();
}

loadMatchesPage(){
this.dataSource.loadMatches('TODO', '', 'asc', this.paginator.pageIndex, this.paginator.pageSize);
}

ngOnDestroy(){
console.log("onDestroy is called");
this.ngUnsubscribe.next();
this.ngUnsubscribe.complete();
}
}

这是相应的 html:

<script type='text/javascript' src='http://d3js.org/d3.v3.min.js'></script>
<script type='text/javascript' src='https://cdn.firebase.com/v0/firebase.js'></script>
<script type='text/javascript' src='d3fire.min.js'></script>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.js?2.1.3"></script>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.geom.js?2.1.3"></script>
<script type="text/javascript" src="http://mbostock.github.com/d3/d3.layout.js?2.1.3"></script>
<br>
<div class="" *ngIf='user'>
<div class="spinner-container" *ngIf="loading$">
<mat-spinner id="spinner"></mat-spinner>
</div>
<mat-table #table [dataSource]="dataSource" class="mat-elevation-z8" *ngIf="!loading$">
<!-- TODO add paidStatus to ngIf -->
<ng-container matColumnDef="rank">
<mat-header-cell *matHeaderCellDef> Rank </mat-header-cell>
<mat-cell *matCellDef="let entry"> {{entry.rank}} </mat-cell>
</ng-container>
<ng-container matColumnDef="weightClass">
<mat-header-cell *matHeaderCellDef> Weight Class </mat-header-cell>
<mat-cell *matCellDef="let entry"> {{entry.weightClass}} </mat-cell>
</ng-container>
<ng-container matColumnDef="ageClass">
<mat-header-cell *matHeaderCellDef> Age Class </mat-header-cell>
<mat-cell *matCellDef="let entry"> {{entry.ageClass}} </mat-cell>
</ng-container>
<ng-container matColumnDef="athlete1Name">
<mat-header-cell *matHeaderCellDef> Athlete 1 </mat-header-cell>
<mat-cell *matCellDef="let entry"> {{entry.athlete1Name}} </mat-cell>
</ng-container>
<ng-container matColumnDef="athlete2Name">
<mat-header-cell *matHeaderCellDef> Athlete 2 </mat-header-cell>
<mat-cell *matCellDef="let entry"> {{entry.athlete2Name}} </mat-cell>
</ng-container>
<ng-container matColumnDef="gender">
<mat-header-cell *matHeaderCellDef> Gender </mat-header-cell>
<mat-cell *matCellDef="let entry"> {{entry.gender}} </mat-cell>
</ng-container>
<ng-container matColumnDef="location">
<mat-header-cell *matHeaderCellDef> Location </mat-header-cell>
<mat-cell *matCellDef="let entry"> {{entry.location}} </mat-cell>
</ng-container>
<ng-container matColumnDef="tournamentName">
<mat-header-cell *matHeaderCellDef> Tournament </mat-header-cell>
<mat-cell *matCellDef="let entry"> {{entry.tournamentName}} </mat-cell>
</ng-container>
<ng-container matColumnDef="date">
<mat-header-cell *matHeaderCellDef> Date </mat-header-cell>
<mat-cell *matCellDef="let entry"> {{entry.date | date:'shortDate'}} </mat-cell>
</ng-container>
<ng-container matColumnDef="videoUrl">
<mat-header-cell *matHeaderCellDef> Video </mat-header-cell>
<mat-cell *matCellDef="let entry"> <a href='{{entry.videoUrl}}'>Click</a> </mat-cell>
<!-- <i class="material-icons right">video</i> -->
</ng-container>
<ng-container matColumnDef="matchRating">
<mat-header-cell *matHeaderCellDef> Match Rating </mat-header-cell>
<mat-cell *matCellDef="let entry"> {{entry.matchRating}}</mat-cell>
<!-- <i class="material-icons right">video</i> -->
</ng-container>

<mat-header-row *matHeaderRowDef="columnsToDisplay"></mat-header-row>
<mat-row *matRowDef="let row; columns: columnsToDisplay;"></mat-row>
</mat-table>

<mat-paginator [length]='matchCount' [pageSize]="2" [pageSizeOptions]="[2, 5, 10]"></mat-paginator>
</div>

最佳答案

根据您的模板,由于父 div 中的 *ngif 指令,您在尝试获取 DOM 中不存在的 ViewChild fir 元素时遇到问题。

有一些选项可以解决这个问题:

  1. 您可以将 *ngIf 指令替换为 [hidden],它会隐藏您的父 div,但将其保留在 dom 中,因此您的 ViewChild 引用将起作用

  2. 如果您必须坚持 *ngIf,则更复杂 - 是确保您的子元素可以作为 ElementRef 抓取(为它分配 id 并在 ngIf 设置为 true 创建后获取对它的引用)并使用ViewChild 的 setter ,如本期所示:

https://stackoverflow.com/a/41095677

关于angular - 制作 Material 分页器时无法读取未定义的属性 'page',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50958115/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com