Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Http 将API数据从一个页面传递到另一个页面(Ionic 2&x2B;Angular 2)_Http_Angular_Typescript_Ionic Framework_Ionic2 - Fatal编程技术网

Http 将API数据从一个页面传递到另一个页面(Ionic 2&x2B;Angular 2)

Http 将API数据从一个页面传递到另一个页面(Ionic 2&x2B;Angular 2),http,angular,typescript,ionic-framework,ionic2,Http,Angular,Typescript,Ionic Framework,Ionic2,我目前正在构建一个配方应用程序,我有一个从API中提取的配方列表。为了实现这一点,我做了一个http get请求,如下所示。我想从JSON中提取{{{id}},并将其放置在(id)所在的url中 loadDetails(id) { if (this.details) { return Promise.resolve(this.details); } return new Promise(resolve => { this.http.get('http://ap

我目前正在构建一个配方应用程序,我有一个从API中提取的配方列表。为了实现这一点,我做了一个http get请求,如下所示。我想从JSON中提取
{{{id}}
,并将其放置在(id)所在的url中

loadDetails(id) {
  if (this.details) {
    return Promise.resolve(this.details);
  }

  return new Promise(resolve => {
    this.http.get('http://api.yummly.com/v1/api/recipe/' + (id) + '?_app_id=397aed16&_app_key=69e2565adcec7a6609b18bef31261e62')
      .map(res => res.json())
      .subscribe(data => {
        console.log(data);
        this.details = data;
        resolve(this.details);
      });
  });
}
通过执行此操作,我在控制台中得到此错误,(id)未定义

我试图加载食谱的.ts文件如下所示

  id: any;

  loadRecipes(){
    this.apiAuthentication.loadDetails(this.id)
    .then(data => {
      this.api = data;
    });
  } 

从代码中得到的任何帮助都是非常好的

您的
id
似乎没有初始化,这就是为什么它是
未定义的
。正确初始化它,然后您将能够获得结果

e、 g


它可能应该是:
this.http.get('http://api.yummly.com/v1/api/recipe/“+id+”?“\u-app\u-id=397aed16和\u-app\u-key=69e2565adcec7a6609b18bef31261e62”)
并检查您在
id
中是否真的有值:)@AJT\u 82我已经删除了括号,尽管仍然未定义id是否有值,您检查过了吗?谢谢,我将如何从JSON(API)中提取一些数据,例如{{recipe.id}}我想您必须调用另一个方法才能获得id。
loadRecipes(){
    this.id = 'someValue';
    this.apiAuthentication.loadDetails(this.id)
    .then(data => {
      this.api = data;
    });
  }