首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未从Web调用API

未从Web调用API
EN

Stack Overflow用户
提问于 2019-04-08 00:44:11
回答 2查看 46关注 0票数 0

作为练习,我正在尝试从API调用一些数据来构建一个基本的天气应用程序。

我在chrome浏览器上使用inspect元素来查看控制台是否会在日志中显示我的数据,但无济于事。

我遇到了一个障碍,我觉得我只是遗漏了一些代码或做了一些小错误。任何帮助都将不胜感激。

下面的代码来自我的weather.ts提供程序

代码语言:javascript
复制
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页面

代码语言:javascript
复制
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);是我的终极目标。以使其按预期显示在控制台中。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-04-08 00:55:53

您拼写错误的生命周期方法名称。

您可能希望使用ionViewWillEnterngOnInit

票数 0
EN

Stack Overflow用户

发布于 2019-04-08 12:09:07

Angular有生命周期方法。NgonInit最初将被调用,因此您需要将您的代码放入onInit方法中。请参阅此处的其他生命周期挂钩https://angular.io/guide/lifecycle-hooks

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55561376

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档