API 未从 Web 调用
API not being called from Web
我正在尝试从 API 中调用一些数据来构建一个基本的天气应用程序作为练习。
我正在 chrome 浏览器上使用检查元素来查看控制台是否会在日志中显示我的数据,但无济于事。
我在这方面遇到了障碍,我觉得我只是遗漏了一些代码或做了一些小错误。
任何帮助,将不胜感激。
下面的代码来自我的 weather.ts 供应商
constructor(public http: HttpClient)
{
console.log('Hello WeatherProvider Provider');
//this.url = 'http://api.openweathermap.org/data/2.5/weather?q=dublin,Ireland&APPID=b90245073a8392aec69a261861286c3b';
}
// getting weather info from API with a custom city and country
getWeather(city, country):Observable<any>
{
return this.http.get('https://api.openweathermap.org/data/2.5/weather?q='+city+','+country+'&APPID='+this.apiKey);
}
}
构造函数中的 console.log 出现在控制台中,但没有进一步的内容。
代码
以下来自我的 home.ts 页面
export class HomePage
{
weather: any;
location:
{
city: string,
country: string
}
constructor(public navCtrl: NavController, private weatherProvider:WeatherProvider)
{
}
ionWillEnter()
{
this.location = {city: 'dublin', country: 'Ireland'}
this.weatherProvider.getWeather(this.location.city, this.location.country).subscribe(weather => {
console.log(weather);
});
}
}
console.log(天气);是我的最终目标。让它按预期显示在控制台中。
您拼错了生命周期方法名称。
您可能想使用 ionViewWillEnter
或 ngOnInit
Angular 有生命周期方法。
NgonInit 最初将被调用,因此您需要将您的代码放在 onInit 方法中。
其他生命周期钩子参考这里
https://angular.io/guide/lifecycle-hooks
我正在尝试从 API 中调用一些数据来构建一个基本的天气应用程序作为练习。
我正在 chrome 浏览器上使用检查元素来查看控制台是否会在日志中显示我的数据,但无济于事。
我在这方面遇到了障碍,我觉得我只是遗漏了一些代码或做了一些小错误。 任何帮助,将不胜感激。
下面的代码来自我的 weather.ts 供应商
constructor(public http: HttpClient)
{
console.log('Hello WeatherProvider Provider');
//this.url = 'http://api.openweathermap.org/data/2.5/weather?q=dublin,Ireland&APPID=b90245073a8392aec69a261861286c3b';
}
// getting weather info from API with a custom city and country
getWeather(city, country):Observable<any>
{
return this.http.get('https://api.openweathermap.org/data/2.5/weather?q='+city+','+country+'&APPID='+this.apiKey);
}
}
构造函数中的 console.log 出现在控制台中,但没有进一步的内容。
代码 以下来自我的 home.ts 页面
export class HomePage
{
weather: any;
location:
{
city: string,
country: string
}
constructor(public navCtrl: NavController, private weatherProvider:WeatherProvider)
{
}
ionWillEnter()
{
this.location = {city: 'dublin', country: 'Ireland'}
this.weatherProvider.getWeather(this.location.city, this.location.country).subscribe(weather => {
console.log(weather);
});
}
}
console.log(天气);是我的最终目标。让它按预期显示在控制台中。
您拼错了生命周期方法名称。
您可能想使用 ionViewWillEnter
或 ngOnInit
Angular 有生命周期方法。 NgonInit 最初将被调用,因此您需要将您的代码放在 onInit 方法中。 其他生命周期钩子参考这里 https://angular.io/guide/lifecycle-hooks