【问题标题】:MEAN SATCK angular2:get data from multiple api in typescriptMEAN STACK angular 2:从打字稿中的多个api获取数据
【发布时间】:2018-08-01 13:31:46
【问题描述】:
  1. 使用 angular2、nodejs、expressjs、mongodb。
  2. 我希望所有 api 都返回数据并显示在 html 页面上。
  3. 这是我的 .ts 文件代码。 enter image description here

    • 所有经过测试的 apis 都可以正常返回 mongodb 的数据。
    • 但第一个 api 返回数据并显示在 html 页面上。
    • 在这种情况下,countphones api 会重新调整数据,而其他两个则不会。
    • 但是当第一个被评论时,第二个开始在 html 上显示数据 页面。
    • 此案先到先得。

【问题讨论】:

  • return 表示这是代码运行的最后一件事。它将忽略 return 块之后的内容。
  • 那么如何处理。

标签: node.js angular typescript mean-stack


【解决方案1】:
export class AppComponent  {

phone$ = http.get("...").map(r => r.json())
laptop$ = http.get("...").map(r => r.json())
television$ = http.get("...").map(r => r.json())

ngOnInit(){
    Observable
    .forkJoin(this.phone$, this.laptop$, this.television$)
    .subscribe(([phones, laptops, televisions])=>{
        this.phones = phones;
        this.laptios = laptops;
        this.televisions = televisions;
  });
}
}

【讨论】:

    猜你喜欢
    • 2017-04-13
    • 1970-01-01
    • 2017-02-13
    • 2016-04-27
    • 2017-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-02
    相关资源
    最近更新 更多