Angular 12 table header 排序未定义
Angular 12 table header sort is undefined
我正在从 Angular 8 升级到 12,但遇到了一些错误。他们中的大多数是固定的。只剩这一个了
The error
此错误表示 sortChange
未定义。数据显示在table中。我将此代码用于排序功能:
ngOnInit(): void {
this.currentPageIndex = 0;
this.currentPageSize = this.paginationSettings.defaultPageSize;
this.setDataSource(this.entities);
this.sort.sortChange.subscribe((sort: Sort) => {
debugger;
return this.sortChanged(sort);
});
}
加载视图时我设置了事件this.sort
ngAfterViewInit() {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
变量的值
@ViewChild(MatTable, { static: true }) table: MatTable<any>;
@ViewChild(MatPaginator) paginator: MatPaginator;
@ContentChild(MatSort, { static: true }) sort: MatSort;
@ContentChildren(MatHeaderRowDef) headerRowDefs!: QueryList<MatHeaderRowDef>;
@ContentChildren(MatRowDef) rowDefs!: QueryList<MatRowDef<TEntity>>
@ContentChildren(MatColumnDef, { descendants: true }) columnDefs!: QueryList<MatColumnDef>;
@Input() entities: TEntity[]; (the data source)
HTML:
<!-- user component -->
<lq-list
matSort
(settingsChanged)="listSettingsChanged.emit($event)"
[entities]="users"
[isLoading]="isLoading"
[displayedColumns]="displayedColumns"
[paginationSettings]="paginationSettings"
[totalEntityCount]="totalUserCount"
>
<ng-container matColumnDef="email">
<mat-header-cell *matHeaderCellDef mat-sort-header> Email </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.email}} </mat-cell>
</ng-container>
<ng-container matColumnDef="firstName">
<mat-header-cell *matHeaderCellDef mat-sort-header> Voornaam </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.firstName}} </mat-cell>
</ng-container>
<ng-container matColumnDef="lastName">
<mat-header-cell *matHeaderCellDef mat-sort-header> Achternaam </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.lastName}} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;" [routerLink]="['/users/', row.id]" class="pointer"></mat-row>
</lq-list>
<!-- the container-->
<mat-table [dataSource]="dataSource">
<ng-content></ng-content>
</mat-table>
<lq-paginator
(page)="paginationSettingsChanged($event)"
[totalEntityCount]="totalEntityCount"
[paginationSettings]="paginationSettings"
></lq-paginator>
ts table 组件
import { Component, Input, EventEmitter, Output, ViewChild, OnInit, AfterViewInit, SimpleChanges, OnChanges } from '@angular/core';
import { User } from '@app/users/models/user';
import { PaginationSettings } from '@app/core/constants/list-pagination-settings';
import { ListSettingsChangedEvent } from '@app/core/models/list-settings-changed-event';
@Component({
selector: 'lq-user-list',
templateUrl: './user-list.component.html',
styleUrls: ['./user-list.component.scss'],
})
export class UsersListComponent {
/**
* The user entities to display
* @type {User[]}
* @memberof UsersListComponent
*/
@Input() users: User[];
/**
* Indicator whether the list is loading
* @type {boolean}
* @memberof UsersListComponent
*/
@Input() isLoading: boolean;
/**
* Pagination settings
* @type {PaginationSettings}
* @memberof UsersListComponent
*/
@Input() paginationSettings: PaginationSettings;
/**
* The total user count
* @type {number}
* @memberof UsersListComponent
*/
@Input() totalUserCount: number;
/**
* The event emitter for when the list settings have changed
* @type {EventEmitter<ListSettingsChangedEvent>}
* @memberof UsersListComponent
*/
@Output() listSettingsChanged: EventEmitter<ListSettingsChangedEvent> = new EventEmitter<ListSettingsChangedEvent>();
/**
* The columns to display
* @type {string[]}
* @memberof UsersListComponent
*/
displayedColumns: string[] = ['email', 'firstName', 'lastName'];
}
@ContentChild(MatSort, { static: true }) sort: MatSort;
内容初始化完成后设置
因此 this.sort
在 ngOnInit()
中未定义。将其移动到 ngAfterContentInit()
.
另外 matSort
指令需要成为投影内容的一部分。
<lq-list
(settingsChanged)="listSettingsChanged.emit($event)"
[entities]="users"
[isLoading]="isLoading"
[displayedColumns]="displayedColumns"
[paginationSettings]="paginationSettings"
[totalEntityCount]="totalUserCount"
>
<ng-container matSort>
<ng-container matColumnDef="email">
<mat-header-cell *matHeaderCellDef mat-sort-header> Email </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.email}} </mat-cell>
</ng-container>
...
</ng-container>
</lq-list>
我正在从 Angular 8 升级到 12,但遇到了一些错误。他们中的大多数是固定的。只剩这一个了 The error
此错误表示 sortChange
未定义。数据显示在table中。我将此代码用于排序功能:
ngOnInit(): void {
this.currentPageIndex = 0;
this.currentPageSize = this.paginationSettings.defaultPageSize;
this.setDataSource(this.entities);
this.sort.sortChange.subscribe((sort: Sort) => {
debugger;
return this.sortChanged(sort);
});
}
加载视图时我设置了事件this.sort
ngAfterViewInit() {
this.dataSource.paginator = this.paginator;
this.dataSource.sort = this.sort;
}
变量的值
@ViewChild(MatTable, { static: true }) table: MatTable<any>;
@ViewChild(MatPaginator) paginator: MatPaginator;
@ContentChild(MatSort, { static: true }) sort: MatSort;
@ContentChildren(MatHeaderRowDef) headerRowDefs!: QueryList<MatHeaderRowDef>;
@ContentChildren(MatRowDef) rowDefs!: QueryList<MatRowDef<TEntity>>
@ContentChildren(MatColumnDef, { descendants: true }) columnDefs!: QueryList<MatColumnDef>;
@Input() entities: TEntity[]; (the data source)
HTML:
<!-- user component -->
<lq-list
matSort
(settingsChanged)="listSettingsChanged.emit($event)"
[entities]="users"
[isLoading]="isLoading"
[displayedColumns]="displayedColumns"
[paginationSettings]="paginationSettings"
[totalEntityCount]="totalUserCount"
>
<ng-container matColumnDef="email">
<mat-header-cell *matHeaderCellDef mat-sort-header> Email </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.email}} </mat-cell>
</ng-container>
<ng-container matColumnDef="firstName">
<mat-header-cell *matHeaderCellDef mat-sort-header> Voornaam </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.firstName}} </mat-cell>
</ng-container>
<ng-container matColumnDef="lastName">
<mat-header-cell *matHeaderCellDef mat-sort-header> Achternaam </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.lastName}} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;" [routerLink]="['/users/', row.id]" class="pointer"></mat-row>
</lq-list>
<!-- the container-->
<mat-table [dataSource]="dataSource">
<ng-content></ng-content>
</mat-table>
<lq-paginator
(page)="paginationSettingsChanged($event)"
[totalEntityCount]="totalEntityCount"
[paginationSettings]="paginationSettings"
></lq-paginator>
ts table 组件
import { Component, Input, EventEmitter, Output, ViewChild, OnInit, AfterViewInit, SimpleChanges, OnChanges } from '@angular/core';
import { User } from '@app/users/models/user';
import { PaginationSettings } from '@app/core/constants/list-pagination-settings';
import { ListSettingsChangedEvent } from '@app/core/models/list-settings-changed-event';
@Component({
selector: 'lq-user-list',
templateUrl: './user-list.component.html',
styleUrls: ['./user-list.component.scss'],
})
export class UsersListComponent {
/**
* The user entities to display
* @type {User[]}
* @memberof UsersListComponent
*/
@Input() users: User[];
/**
* Indicator whether the list is loading
* @type {boolean}
* @memberof UsersListComponent
*/
@Input() isLoading: boolean;
/**
* Pagination settings
* @type {PaginationSettings}
* @memberof UsersListComponent
*/
@Input() paginationSettings: PaginationSettings;
/**
* The total user count
* @type {number}
* @memberof UsersListComponent
*/
@Input() totalUserCount: number;
/**
* The event emitter for when the list settings have changed
* @type {EventEmitter<ListSettingsChangedEvent>}
* @memberof UsersListComponent
*/
@Output() listSettingsChanged: EventEmitter<ListSettingsChangedEvent> = new EventEmitter<ListSettingsChangedEvent>();
/**
* The columns to display
* @type {string[]}
* @memberof UsersListComponent
*/
displayedColumns: string[] = ['email', 'firstName', 'lastName'];
}
@ContentChild(MatSort, { static: true }) sort: MatSort;
内容初始化完成后设置
因此 this.sort
在 ngOnInit()
中未定义。将其移动到 ngAfterContentInit()
.
另外 matSort
指令需要成为投影内容的一部分。
<lq-list
(settingsChanged)="listSettingsChanged.emit($event)"
[entities]="users"
[isLoading]="isLoading"
[displayedColumns]="displayedColumns"
[paginationSettings]="paginationSettings"
[totalEntityCount]="totalUserCount"
>
<ng-container matSort>
<ng-container matColumnDef="email">
<mat-header-cell *matHeaderCellDef mat-sort-header> Email </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.email}} </mat-cell>
</ng-container>
...
</ng-container>
</lq-list>