gpt4 book ai didi

angular - MatSort 未定义 - Angular 5

转载 作者:太空狗 更新时间:2023-10-29 17:51:18 25 4
gpt4 key购买 nike

我正在尝试在我的 Angular 应用程序中实现一个 Material 表。分页和过滤器工作正常,但我无法对我的表格进行排序。我对 MatSort 的引用是未定义的。

我确实在 AppModule 中导入了它:

import {MatTableModule} from '@angular/material/table';
import {MatTooltipModule} from '@angular/material/tooltip';
import {MatButtonModule, MatCheckboxModule,MatPaginatorModule,MatInputModule} from '@angular/material';
import {MatSortModule} from '@angular/material/sort';

...
@NgModule({
declarations: [...],
imports: [
MatSortModule,
MatTableModule,
MatPaginatorModule,
MatInputModule,
...
]
})

这是我的 component.ts:

import { Component, OnInit , ViewChild, AfterViewInit} from '@angular/core';
...
import {MatTableDataSource,MatSort,MatPaginator} from '@angular/material';
...
export class MyClass inplements OnInit, AfterViewInit{
@ViewChild(MatSort) sort:MatSort;
@ViewChild(MatPaginator) paginator:MatPaginator;
...
ngAfterViewInit(){
this.dataSource = new MatTableDataSource(this.fake_data);
this.dataSource.paginator = this.paginator
this.dataSource.sort = this.sort
}
...
}

这是我的组件 HTML:

<mat-form-field *ngIf="ready" class="width-80">
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Filter">
</mat-form-field>
<mat-table #table class = "mat-elevation-z8 animate" matSort [dataSource]="dataSource" display:flex>
<ng-container matColumnDef="fake_name">
<mat-header-cell *matHeaderCellDef mat-sort-header class="columnTitle"><b>{{fake_label.name}}</b></mat-header-cell>
<mat-cell *matCellDef="let fake;" class="cellContent">{{fake.name}}</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="fakeColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: fakeColumns" class="animate"></mat-row>
</mat-table>
<mat-paginator [length]="length" [pageSize]="5" [pageSizeOptions]="[5,10,15,20,25,50,100]" showFirstLastButtons></mat-paginator>

有人知道我做错了什么吗?

最佳答案

问题可以通过添加 apps.module.ts 来解决:

import { MatPaginatorModule,MatSortModule } from '@angular/material';

和:

imports: [
....
MatPaginatorModule,
MatSortModule

希望这对您有所帮助。

关于angular - MatSort 未定义 - Angular 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50722013/

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