[英]I get a error while publishing a app. The error is property json does not exist on type object
当我尝试在Ionic serve命令中测试应用程序时,没有收到任何错误。 但是,当我尝试发布应用程序时,出现错误,因为“类型object上不存在属性json”。 该错误发生在转换阶段:
如何解决这个问题呢? 我尝试了所有可能的方法,但是并没有解决我的问题。
Home.ts
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { WeatherProvider } from '../../providers/weather/weather';
import { Storage } from '@ionic/storage';
//import { Response } from '@angular/http';
//import 'rxjs/add/operator/map';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
weather:any;
location:{
city:string,
state:string
}
constructor(
public navCtrl: NavController,
private weatherProvider:WeatherProvider,
private storage: Storage) {
}
ionViewWillEnter(){
this.storage.get('location').then((val)=>{
if(val!=null){
this.location = JSON.parse(val);
}else{
this.location = {
city: 'Chennai',
state: 'TN'
}
}
this.weatherProvider.getWeather(this.location.city,this.location.state)
// .map((res: Response) => res.json() )
.subscribe(weather => {
this.weather = weather.current_observation;
});
});
}
}
天气
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
//import { Response } from '@angular/http';
//import 'rxjs/add/operator/map';
//import 'rxjs/Rx';
@Injectable()
export class WeatherProvider {
apiKey = '6d3243fb22b01d0c';
url;
constructor(public http: HttpClient) {
console.log('Hello WeatherProvider Provider');
this.url = 'http://api.wunderground.com/api/'+this.apiKey+'/conditions/q';
}
getWeather(city, state){
return this.http.get(this.url+'/'+state+'/'+city+'.json')
// .map(res => res.json());
// .map((res: Response) => res.json() );
}
}
打字稿就是关于打字的。 因此,您应该声明从getWeather方法接收的对象的类型。 首先在home.ts的末尾创建一个Weather类(请看下面)
class Weather {
current_observation: string;
}
并进行以下更改:
this.weatherProvider.getWeather(this.location.city,this.location.state)
// .map((res: Response) => res.json() )
.subscribe((weather: Weather) => {
this.weather = weather.current_observation;
});
});
}
ionViewWillEnter(){
this.storage.get('location').then((val) => {
if(val != null){
this.location = JSON.parse(val);
} else{
this.location ={
city: 'miami',
state: 'FL'
}
}
//尝试下面的代码this.weatherprovider.getweather(this.location.city,this.location.state).subscribe(result => {let weather:any = result; this.weather = weather.current_observation; console.log(this .weather);}); }); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.