gpt4 book ai didi

angular - 如何将 MatPaginator 初始化为 @ViewChild 成员变量?

转载 作者:行者123 更新时间:2023-12-03 20:10:54 28 4
gpt4 key购买 nike

我在 Visual Studio Code 一个 Angular 8 项目中编码,刚刚添加了一些严格模式配置:

"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"noImplicitThis": true,
"alwaysStrict": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,

现在,我的分页器工作正常,现在甚至没有编译。

我可以使用以下命令实例化 MatSort:
@ViewChild(MatSort, { static: true }) sort: MatSort = new MatSort();

但是我不能对 MatPaginator 成员变量做同样的事情:
@ViewChild(MatPaginator, { static: true }) paginator?: MatPaginator;

这迫使我在代码中添加一些检查:
if (this.paginator) {

我阅读了这篇博客 article,但我仍在寻找替代解决方案。

最佳答案

使用 new MatPaginator() 实例化时,错误消息说:

Expected 2-3 arguments, but got 0.ts(2554)


由于 MatPaginator 具有非可选参数,您必须使用参数进行初始化。第一个参数是一个 MapPaginatorIntl 类;第二个是 ChangeDetectorRef 抽象类,无法实例化,但是可以调用 ChangeDetectorRef.prototype 去掉错误信息,如下:
@ViewChild(MatPaginator) paginator: MatPaginator = new MatPaginator(new MatPaginatorIntl(), ChangeDetectorRef.prototype);

关于angular - 如何将 MatPaginator 初始化为 @ViewChild 成员变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60073541/

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