【发布时间】:2021-08-21 15:43:31
【问题描述】:
如何将 C# 字典正确传递到 typescript Map。
[HttpGet("reportsUsage")]
public IActionResult GetReportsUsage()
{
//var reportsUsage = _statService.GetReportsUsage();
IDictionary<int, int> test = new Dictionary<int, int>();
test.Add(1, 20);
test.Add(2, 30);
test.Add(3, 40);
test.Add(4, 50);
test.Add(5, 70);
test.Add(6, 60);
test.Add(7, 90);
test.Add(8, 30);
return Ok(test);
//return Ok(reportsUsage );
}
角度:
getReportsUsage() {
return this.http.get<Map<number, number>>(`${environment.apiUrl}/stats/reportsUsage`, {
headers: new HttpHeaders({
'Content-Type': 'text/plain',
'Accept': 'application/json'
}),
withCredentials: true
});
}
reportsUsage = new Map<number, number>();
this.statsService.getReportsUsage().subscribe(data => {
this.reportsUsage = data;
//1
console.log(this.reportsUsage);
//2
console.log(this.reportsUsage.values());
//3
console.log(typeof(this.reportsUsage));
};
结果:
1
{1:20、2:30、3:40、4:50、5:70、6:60、7:90、8:30}
2
ERROR TypeError: this.reportsUsage.values 不是函数
3
对象
所以数据类型从字典变成了对象,我尝试用下面的方法转换它,但还是不行:
console.log(new Map(this.reportsUsage));
TypeError: object is not iterable (cannot read property Symbol(Symbol.iterator))
【问题讨论】:
标签: javascript angular typescript asp.net-core asp.net-web-api