简体   繁体   English

Angular - 类型“”上不存在属性“”

[英]Angular - Property ' ' does not exist on type ' '

I'm a beginner to angular and am taking an online course on edx.我是 angular 的初学者,正在学习 edx 的在线课程。 In one of the tutorial labs, I am learning how to use the HttpClient .在其中一个教程实验室中,我正在学习如何使用HttpClient

ngOnInit() {
   this.GitSearchService.gitSearch('angular').then( (response) => {
    alert("Total Libraries Found:" + response.total_count);
   }, (error) => {
    alert("Error: " + error.statusText)
   })
 }
 title = 'app is functional!';
}

It says total_count does not exist on type unknown, but in one of the imports, I have "total_count": number它说total_count在未知类型上不存在,但在其中一个导入中,我有"total_count": number

Problem is that the response variable doesn't have a type.问题是response变量没有类型。
You can fix that in different ways, some cleaner, some less.你可以用不同的方式来解决这个问题,一些更干净,一些更少。

An unclean way, for example, would be to workaround the typing例如,一种不干净的方法是解决打字问题

alert("Total Libraries Found:" + response['total_count']);

A more clean way would be to provide a type to the response一种更简洁的方法是为响应提供一个类型

this.GitSearchService.gitSearch('angular')
  .then( (response : MyAwesomeType) => { your code } )

in this case, MyAwesomeType is expected to be a type that has the property total_count在这种情况下, MyAwesomeType应该是具有属性total_count的类型

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM