【发布时间】:2018-06-14 06:50:52
【问题描述】:
当我尝试在 Ionic serve 命令中测试应用程序时,我没有收到任何错误。但是当我尝试发布应用程序时,我收到错误,因为“类型对象上不存在属性 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;
});
});
}
}
Weather.ts
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() );
}
}
【问题讨论】:
-
你在用 angular 5 吗?
-
我不知道兄弟刚刚从这个link学习教程
-
查看您的 package.json 并查看您使用的版本。
-
是的,我正在使用 Angular 5.0.3 版本
-
@halfer ok halfer
标签: json angular typescript ionic-framework ionic3