angular - 为什么角度将具有不同属性名称的结果集分配给具有不同名称的接口?它是如何工作的?
问题描述
Angular 很奇怪,还是我来自后端背景发现它很奇怪。
看,我有这个角度的界面
export interface apiHero
{
Id: number,
Nam: string
}
然后我给它分配从 webAPI 返回的东西。
在组件中,
HeroesWebAPI: apiHero[]= [];
getHeroes(): void
{
console.log("getHeroes called");
this.heroService.getHeroesFromWebAPI().subscribe( r => {this.HeroesWebAPI= r;
console.log("Final hit:"+ this.HeroesWebAPI)});
}
服务:
getHeroesFromWebAPI(): Observable<any>
{
return this.http.get<any>("https://localhost:44320/api/values").pipe(
map(res => JSON.stringify(res))
);
}
现在奇怪的部分,注意界面?它有Id
和Nam
属性,而Nam
不是返回一个具有属性的类的数组和Name
webAPI
Id
Name
public class Hero
{
public int Id { get; set; }
public string Name { get; set; }
}
当我在控制台检查组件功能的结果时,它是
最后一击:[{"Id":1,"Name":"Johns"},{"Id":2,"Name":"Tonny"}]
它是如何映射到具有不同属性的接口的?
解决方案
推荐阅读
- javascript - 如何禁用锚标记中某些元素的重定向?
- c++ - C++问题,如何查看packing的默认值
- python - 混淆 CosmosDB 的分区键
- mysql - mysql根据值加入不同的表
- angular - 未捕获的错误:Angular 12 中的 JIT 编译器不可用
- java - 将消费者泛型类型转换为另一种类型
- javascript - 如何避免 Select2 触发更改,但将选项修复为数据库中的特定值?
- javascript - 在 JavaScript/TypeScript 中获取日期字符串的格式字符串
- c# - 在添加新项目中输入错误后“Package EntityFramework 5.0.0 已恢复”
- javascript - 检查选择字段中的值 - 更改禁用状态