我遇到如下问题:
我有 user-details-with-addresses.ts 文件,它有两个属性:
export class userDetailsWithAddresses{
user: User;
addresses: UserWithAddresses[];
}
我有 user-component.ts,我可以在其中调用服务并获取 userDetailsWithAddresses[] 类型的响应。
export class userComponent implements OnInit {
data: userDetailsWithAddresses[] = null;
getArchiveUserDetails(id: string){
this.userService.getUserListWithAddressById(id).subscribe(response => {
console.log(response);
this.data=response;
console.log(this.data[0]);
console.log(this.data.user);
});
}
}
我确实在 Chrome 开发者工具中看到了响应。我什至在开发者工具中看到了 this.data.user 值,它是 User 对象。
this.data.addresses 有三个地址对象。我想读取用户和地址列表。但是,当我如上所示检查 this.data[0] 时,它是未定义的。
当我在上面的代码中检查 this.data.user 时,它说“userDetailsWithAddresses[]”类型上不存在属性“user”。
请帮我解决这个问题。
您应该将
data
设置为仅界面 userDetailsWithAddresses
export class userComponent implements OnInit { data: userDetailsWithAddresses = null;