【发布时间】:2018-06-13 14:04:48
【问题描述】:
我像这样使用Angular Material table component:
this.userService.getUsers().subscribe((response) => {
this.users = new MatTableDataSource(response);
});
它可以工作,但是在编译时会抛出以下打字错误:
“对象”类型的参数不能分配给类型参数 '{}[]'。 “对象”类型可分配给极少数其他类型。做过 您的意思是改用“任何”类型?缺少属性“包含” 在类型“对象”中。
所以我尝试将其更改为:
this.users = new MatTableDataSource<any>(response);
但还是同样的错误。 response 看起来像这样:
[{
userId: 123,
username: 'Tom'
}, {
userId: 456,
username: 'Joe'
}]
知道如何摆脱错误吗?
编辑
如果我这样做,应该提及:
this.users = new MatTableDataSource([response]);
错误消失了,但表格无法正常工作,因为格式不是表格所期望的正确格式。只是注意到这一点,以防它揭示可能是什么原因......
【问题讨论】:
-
你尝试过类型转换吗?例如。
response as any -
您需要更改
subscribe()函数以将正确的(数组)类型传递给其回调参数。 -
@JonasW。是的!这工作
this.users = new MatTableDataSource(<any> response);谢谢
标签: javascript angular typescript