簡體   English   中英

Angular,類型上不存在屬性

[英]Angular, property does not exist on type

我有一個小的天氣應用程序。 在瀏覽器中一切正常,但我從 TS 編譯器收到錯誤:屬性namemainwind在類型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 編譯器停止大喊大叫!

嘗試定義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);
    });
  }

我遇到了同樣的問題,這就是解決方案。 前:

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

    });
  }

后:

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