sch*_*mer 8 angular-material angular
我正在尝试使用 MatSort 模块对 Angular Material 中的表格进行排序,但出现此错误:
Property 'sort' has no initializer and is not definitely assigned in the constructor.
Run Code Online (Sandbox Code Playgroud)
这是我的 ts 文件:
import { Component, OnInit, ViewChild } from '@angular/core';
import { Personal } from 'src/app/models/personal.model';
import { PersonalService } from 'src/app/services/personal.service';
import { MatTableDataSource } from '@angular/material/table';
import { MatPaginatorModule } from '@angular/material/paginator';
import { MatSort, MatSortModule, MatSortHeader } from '@angular/material/sort'
@Component({
selector: 'app-personal-list',
templateUrl: './personal-list.component.html',
styleUrls: ['./personal-list.component.css']
})
export class PersonalListComponent implements OnInit {
personals?: Personal[];
currentPersonal?: Personal;
currentIndex = -1;
name = '';
PERSONAL_DATA : Personal[] = []
displayedColumns: string[] = ['name', 'action'];
dataSource = new MatTableDataSource<Personal>(this.PERSONAL_DATA);
@ViewChild(MatSort) sort: MatSort;
constructor(private personalService: PersonalService) { }
ngOnInit(): void {
this.getAllPersonalTable();
}
private getAllPersonalTable(){
let resp = this.personalService.getAllPersonal();
resp.subscribe(report =>this.dataSource.data=report as Personal[]);
this.dataSource.sort = this.sort;
}
}
Run Code Online (Sandbox Code Playgroud)
错误发生在这一行
@ViewChild(MatSort) sort: MatSort;
Run Code Online (Sandbox Code Playgroud)
是的,我已经尝试过了sort!。这消除了错误,但无法编译我的页面。"strictPropertyInitialization": false,tsconfig.json 文件中也是如此。
非常感谢任何有关如何解决的帮助!
amo*_*mof 12
我遇到的问题和你一模一样。这是因为 Angular 12 在打字稿中启用了严格模式(参考此处)。
尝试使用:
@ViewChild(MatSort, { static: false }) sort!: MatSort;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16458 次 |
| 最近记录: |