简体   繁体   English

Angular,类型上不存在属性

[英]Angular, property does not exist on type

I have a small weather app.我有一个小的天气应用程序。 All works fine in browser, but I'm getting errors from TS compiler: property name , main , wind does not exist on type Weather[] .在浏览器中一切正常,但我从 TS 编译器收到错误:属性namemainwind在类型Weather[]上不存在。 But seems like I added these properties in class Weather[] ...但似乎我在类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 编译器停止大喊大叫!

Try defining data:any in subscribe尝试定义data:any in subscribe

ngOnInit() {    

    this.getWeatherService.getWeather("Misto Kyyiv").subscribe((data: any)=> {
      this.weather = data;
      this.temp = (data.main.temp -273.15).toFixed(2);
      this.pressure = data.main.pressure;
      this.humidity = data.main.humidity;
      this.wind_speed = data.wind.speed;
      this.wind_dec = data.wind.deg;
      this.city = data.name;
      console.log(this.weather);
      console.log(this.temp);
    });
  }

I had the same problem and just that was the solution.我遇到了同样的问题,这就是解决方案。 Before:前:

async getProfessional(){
     await this._ps.getProfessional(idProfessional).subscribe( data => {
        this.professionalSkills = data[0].aptitudes;
        this.technicalSkills = data[0].technologies;

    });
  }

After:后:

async getProfessional(){
     await this._ps.getProfessional(idProfessional).subscribe( (data:any) => {
        this.professionalSkills = data[0].aptitudes;
        this.technicalSkills = data[0].technologies;
    });
  }

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

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