簡體   English   中英

從Angular 6的Api中獲取並合並數據

[英]Fetching and combine data from Api In Angular 6

如何從API檢索數據,並且對於每個用戶ID,他的帖子及其評論都以一個JSON對象返回?

可以從以下API獲取帖子https : //jsonplaceholder.typicode.com/posts

及其來自此API的評論https : //jsonplaceholder.typicode.com/comments

//Get User Posts And Comments
getUser() {
    this.http.get('https://jsonplaceholder.typicode.com/posts') && this.http.get('https://jsonplaceholder.typicode.com/comments')

    .subscribe(data => {
      this.posts = data;
    });

  }

使用forkJoin合並請求

let req1 = this.http.get('https://jsonplaceholder.typicode.com/posts')  
let req2 = this.http.get('https://jsonplaceholder.typicode.com/comments')

forkJoin([req1,req2 ] )
   .subscribe(data => {
      this.posts = data;
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM