首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >TypeError:无法读取未定义的属性-但网站工作正常- Angular 7

TypeError:无法读取未定义的属性-但网站工作正常- Angular 7
EN

Stack Overflow用户
提问于 2018-12-20 03:24:05
回答 1查看 1.5K关注 0票数 1
TypeError: Cannot read property 'city' of undefined

我的应用程序在浏览器中呈现得很好,但我在Chrome控制台中得到了这个错误,我希望了解/修复这个错误。控制台给出了3次错误,指向<span>{{current.city}}, {{current.country}} </span>文件/行CurrentWeatherComponent.html:3

如果我删除{{current.city}},,它会给出错误TypeError: Cannot read property 'country' of undefined Thing is,在浏览器中,应用程序实际上呈现了current城市,current国家,current日期,以及current的所有其他属性。由于错误在控制台中打印了三次,但随后呈现良好,我猜这与时间有关?

下面的代码是完整的。让我知道如果(和什么)我需要张贴更多细节。

HTML:

current-weather.component.html:
<div>
  <div>
    <span>{{current.city}}, {{current.country}} </span>
    <span>{{current.date | date:'fullDate'}}</span>
  </div>
  <div>
    <img [src]='current.image'>
    <span>{{current.temperature | number: '1.0-0'}}℉</span>
  </div>
  <div>
    {{current.description}}
  </div>
</div>

组件:

current-weather.component.ts
import { Component, OnInit } from '@angular/core'
import { ICurrentWeather } from '../interfaces'
import { WeatherService } from '../weather/weather.service'

@Component({
  selector: 'app-current-weather',
  templateUrl: './current-weather.component.html',
  styleUrls: ['./current-weather.component.css'],
})
export class CurrentWeatherComponent implements OnInit {
  current: ICurrentWeather

  constructor(private weatherService: WeatherService) {}

  ngOnInit() {
    this.weatherService
      .getCurrentWeather('Hilversum', 'NL')
      .subscribe(data => (this.current = data))
  }
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53857932

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档