我正在使用角度材质表组件像这样:
this.userService.getUsers().subscribe((response) => {
this.users = new MatTableDataSource(response);
});
它可以工作,但是在编译时会抛出以下打字稿错误:
“对象”类型的参数不可分配给类型的参数
'{}[]'。 “对象”类型可分配给很少的其他类型。做过
您的意思是使用“any”类型来代替?缺少属性“包含”
在“对象”类型中。
所以我尝试将其更改为:
this.users = new MatTableDataSource<any>(response);
但仍然是同样的错误。response
看起来像这样:
[{
userId: 123,
username: 'Tom'
}, {
userId: 456,
username: 'Joe'
}]
知道如何消除错误吗?
EDIT
如果我这样做的话应该提到:
this.users = new MatTableDataSource([response]);
错误消失,但表格无法工作,因为格式不是表格预期的正确格式。只是注意到这一点,以防它揭示可能的原因......
“Object”类型的参数不可分配给“{}[]”类型的参数。
这并不意味着MatTableDataSource
接受错误的参数,而是你的response
类型错误。你绝对应该输入:
(response: {userId: number, username: string }[]) =>
或者在传递它时这样做:
new MatTableDataSource(response as {userId: number, username: string }[])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)