角度,类型上不存在属性

时间:2019-08-08 19:18:04

标签: angular typescript

我有一个小型天气应用。一切在浏览器中都可以正常工作,但是我从TS编译器中遇到了错误:类型Time上不存在属性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;   
}

3 个答案:

答案 0 :(得分:1)

尝试在data:any中定义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);
    });
  }

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我遇到了同样的问题,仅此而已。 之前:

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;
    });
  }