Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html GET请求后访问对象属性时出错?_Html_Angular_Typescript - Fatal编程技术网

Html GET请求后访问对象属性时出错?

Html GET请求后访问对象属性时出错?,html,angular,typescript,Html,Angular,Typescript,我在angular应用程序中使用天气api,这是api的一部分 我通过此功能获取数据: getTempByName(name) { return this.http.get('https://api.weatherbit.io/v2.0/current?' + 'city=' + name + '&key=mykey'); } 然后通过以下代码将其分配给对象,其中weatherData是我的对象: weatherData: object; this.fetch.getTempByNa

我在angular应用程序中使用天气api,这是api的一部分

我通过此功能获取数据:

getTempByName(name) {
return this.http.get('https://api.weatherbit.io/v2.0/current?' + 'city=' + name + 
'&key=mykey');
}
然后通过以下代码将其分配给对象,其中weatherData是我的对象:

weatherData: object;
this.fetch.getTempByName(this.name).subscribe(data => {
  if (data) {
    this.weatherData = data;
  } else {
    this.noty.notify('error', 'The city Name you entered is incorrect');
  }});
}
然后在我的html中,我使用以下代码显示数据:

<div class="container" *ngIf="weatherData">
  <p class="country">{{weatherData.data[0].city_name}}</p>
  <p class="temp">{{weatherData.data[0].temp}}°</p>
</div>

{{weatherData.data[0].city_name}

{{weatherData.data[0].temp}}

当我在本地服务器上运行应用程序时,一切都会顺利运行,但只要我通过以下命令构建应用程序,就会出现以下错误:

src/app/weahter info/weahter info.component.html(8,22)中出错:上不存在属性“data” 键入“object”。 src/app/weahter info/weahter info.component.html(9,8):类型上不存在属性“data” “对象”。 src/app/weahter info/weahter info.component.html(10,31):类型上不存在属性“data” “对象”。 src/app/weahter info/weahter info.component.html(11,19):类型上不存在属性“data” “对象”。 src/app/weahter info/weahter info.component.html(14,73):类型上不存在属性“data” “对象”。 src/app/weahter info/weahter info.component.html(17,54):类型上不存在属性“data” “对象”


我试图通过各种方法找到忽略错误的方法,但我做不到,欢迎提供任何帮助。

weatherData:object更改为
天气数据:任意

值来自API,我们希望退出类型检查,并让值通过编译时检查。为此,我们用
any
类型标记它们,或者您可以创建类或接口来避免它