简体   繁体   中英

Fetching and combine data from Api In Angular 6

How to retrieve data from API and For each user id returns his posts with their comments in one JSON object?

posts can be fetched from this API: https://jsonplaceholder.typicode.com/posts

and their comments from this 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;
    });

  }

use forkJoin to combine requests

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;
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM