我在 angularjs 2 中使用 typescript 语言到我的 REST api 应用程序,问题是 - typescript 给出的编译时错误为
[ts] Property 'name' does not exist on type 'JSON'.any
当我尝试访问传入的 JSON 对象时,我的函数代码如下
createPerformanceChart(data: JSON) {
// Show Up the Fund Details Data
var details: any;
for(var j = 0 ; j < Object.keys(this.funds).length; j++)
{
if(data.name == this.funds[j].name) // data.name throws Error
{
details = this.funds[j];
}
}
}
如何转换 JSON 或访问 JSON 对象 - 这样它就不会抛出编译时错误并让我编译代码。
如果你想创建强类型
您必须更改响应类型(JSON
)到更具体的东西,例如使用界面:
interface IPerformanceData {
name: string;
}
然后使用它作为传入响应的类型:
createPerformanceChart(data: IPerformanceData) {
// you can now use: data.name
}
如果你不在乎
只是让它成为一种类型any
:
createPerformanceChart(data: any) {
// you can now use any property: data.*
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)