angular - TypeScript 泛型类型作为参数
问题描述
请帮我解决这种错误 core.js:6157 ERROR TypeError: Cannot read property 'displayProp' of undefined for this code:
export class Company {
companyName: string;
cif: string;
static tableName: string = 'Companies';
static displayProp: string = 'companyName';
}`enter code here`
export class GenericListComponent<T> implements OnInit {
@Input() listItems: T[] = [];
@Input() modelType: { new(...args: any[]): T; };
constructor() { }
ngOnInit(): void {
console.log(this.listItems);
}
}
generic-list.component.html
<div *ngIf="listItems">
<ul>
<li *ngFor="let listItem of listItems">
<h4>{{listItem[modelType].displayProp}}</h4>
</li>
</ul>
</div>
export class ListExComponent implements OnInit {
companyRef: Company;
companies: Company[] = [
{ companyName: "Comp 1", cif: "123" },
{ companyName: "Comp 2", cif: "456" }
];
constructor() { }
ngOnInit(): void {
}
}
list-ex.component.html
<div>
<app-generic-list #childComponentId [listItems]="companies" [modelType]="companyRef"></app-generic-list>
</div>
app.component.html
<app-list-ex></app-list-ex>
我在这里找到了这段代码: https ://levelup.gitconnected.com/typescript-generic-types-as-parameters-882e692073d5 但是坏了。
谢谢。
解决方案
推荐阅读
- php - 调用未定义的方法 MongoDB\Driver\Cursor::current()
- xml - 为什么 DOMParser 不能解析这个 XML?
- error-handling - SAS - 如果输入数据集中缺少变量,则将错误写入日志
- c++ - 我的 qmake 构建在调试构建中的速度是 cmake 的两倍多,为什么?
- powerbi - 以允许按月过滤销售值的方式显示具有销售值的整个公司商店基础
- sql - SELECT NOT IN 子查询中的多列
- php - 在 for 循环 PHP 中处理变量数据
- javascript - reduce() 从多个对象返回字符串数组
- python - python列表随机抽样所有配置
- java - 运行 Eclipse 项目 x 次,直到发生抛出的错误