Angular,类型上不存在属性

标签 angular typescript

我有一个小的天气应用程序。在浏览器中一切正常,但我从 TS 编译器收到错误:属性 namemainwind 在类型 上不存在>天气[]。但似乎我在 Weather[] 类中添加了这些属性...

export class AppComponent implements OnInit {  
  weather:Weather[];
  temp:string;
  pressure:number;
  humidity:number;
  wind_speed:number;
  wind_dec:number;
  city:string;
  activeId:string = "Misto Kyyiv"; 

  constructor(private getWeatherService:GetWeatherService) {}

  ngOnInit() {    
    this.getWeatherService.getWeather("Misto Kyyiv").subscribe(weather => {
      this.weather = weather;
      this.temp = (weather.main.temp -273.15).toFixed(2);
      this.pressure = weather.main.pressure;
      this.humidity = weather.main.humidity;
      this.wind_speed = weather.wind.speed;
      this.wind_dec = weather.wind.deg;
      this.city = weather.name;
      console.log(this.weather);
      console.log(this.temp);
    });
  }
export class Weather {
    main:any = {};
    wind:any = {};
    temp:number;
    pressure:number;
    humidity:number;
    wind_speed:number;
    wind_dec:number;
    city:string;
    activeId:string; 
    name:string;   
}
  //Get Weather 
  //it worked when I changed here Weather[] to Weather !!!
  getWeather(city:string):Observable<Weather> {
    let key = "c2dcf8ffb5cdc3f8977bfd2ae7ea4738";    
    let url = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "&?units=metric&APPID=" + key;
    return this.http.get<Weather>(url);
  }

最佳答案

我将 Weather[] 更改为 Weather,TS 编译器停止了叫喊!

关于Angular,类型上不存在属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57419341/

相关文章:

angular - 无法读取未定义错误的属性 'navigate'

css - 当导航栏折叠时,下拉菜单在导航栏内 float mdbootstrap

angular - 如何在 Strapi、GraphQL 的 where 子句中使用多个 'and' 条件进行查询?

angularjs - 如何以 Angular 2显示嵌套对象和/或数组

javascript - 扩展在 Typescript-Angular 中没有 Typescript 定义的 Javascript 库

javascript - 将两行连接成一行时自动删除空格

javascript - 如何在 Angular 12 中动态加载组件

使用 babel 转译的项目中的 Angular 依赖注入(inject)

typescript - defaultRowRenderer 打破了 Jest 单元测试。 ...对象。<匿名> ...导入 createMultiSort

扩展泛型时 TypeScript `extend` 产生 ESLint 错误