- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在关注 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 元素时遇到问题。
有一些选项可以解决这个问题:
您可以将 *ngIf 指令替换为 [hidden],它会隐藏您的父 div,但将其保留在 dom 中,因此您的 ViewChild 引用将起作用
如果您必须坚持 *ngIf,则更复杂 - 是确保您的子元素可以作为 ElementRef 抓取(为它分配 id 并在 ngIf 设置为 true 创建后获取对它的引用)并使用ViewChild 的 setter ,如本期所示:
关于angular - 制作 Material 分页器时无法读取未定义的属性 'page',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50958115/
你能比较一下属性吗 我想禁用文本框“txtName”。有两种方式 使用javascript,txtName.disabled = true 使用 ASP.NET, 哪种方法更好,为什么? 最佳答案 我
Count 属性 返回一个集合或 Dictionary 对象包含的项目数。只读。 object.Count object 可以是“应用于”列表中列出的任何集合或对
CompareMode 属性 设置并返回在 Dictionary 对象中比较字符串关键字的比较模式。 object.CompareMode[ = compare] 参数
Column 属性 只读属性,返回 TextStream 文件中当前字符位置的列号。 object.Column object 通常是 TextStream 对象的名称。
AvailableSpace 属性 返回指定的驱动器或网络共享对于用户的可用空间大小。 object.AvailableSpace object 应为 Drive 
Attributes 属性 设置或返回文件或文件夹的属性。可读写或只读(与属性有关)。 object.Attributes [= newattributes] 参数 object
AtEndOfStream 属性 如果文件指针位于 TextStream 文件末,则返回 True;否则如果不为只读则返回 False。 object.A
AtEndOfLine 属性 TextStream 文件中,如果文件指针指向行末标记,就返回 True;否则如果不是只读则返回 False。 object.AtEn
RootFolder 属性 返回一个 Folder 对象,表示指定驱动器的根文件夹。只读。 object.RootFolder object 应为 Dr
Path 属性 返回指定文件、文件夹或驱动器的路径。 object.Path object 应为 File、Folder 或 Drive 对象的名称。 说明 对于驱动器,路径不包含根目录。
ParentFolder 属性 返回指定文件或文件夹的父文件夹。只读。 object.ParentFolder object 应为 File 或 Folder 对象的名称。 说明 以下代码
Name 属性 设置或返回指定的文件或文件夹的名称。可读写。 object.Name [= newname] 参数 object 必选项。应为 File 或&
Line 属性 只读属性,返回 TextStream 文件中的当前行号。 object.Line object 通常是 TextStream 对象的名称。 说明 文件刚
Key 属性 在 Dictionary 对象中设置 key。 object.Key(key) = newkey 参数 object 必选项。通常是 Dictionary 
Item 属性 设置或返回 Dictionary 对象中指定的 key 对应的 item,或返回集合中基于指定的 key 的&
IsRootFolder 属性 如果指定的文件夹是根文件夹,返回 True;否则返回 False。 object.IsRootFolder object 应为&n
IsReady 属性 如果指定的驱动器就绪,返回 True;否则返回 False。 object.IsReady object 应为 Drive&nbs
FreeSpace 属性 返回指定的驱动器或网络共享对于用户的可用空间大小。只读。 object.FreeSpace object 应为 Drive 对象的名称。
FileSystem 属性 返回指定的驱动器使用的文件系统的类型。 object.FileSystem object 应为 Drive 对象的名称。 说明 可
Files 属性 返回由指定文件夹中所有 File 对象(包括隐藏文件和系统文件)组成的 Files 集合。 object.Files object&n
我是一名优秀的程序员,十分优秀!