I have two URLs like this:
private URL="http://127.0.0.1:8000/api/product/?category=3"
private me="http://127.0.0.1:8000/api/product/?category=2"
Currently I'm able to get one of them.
service:
private URL="http://127.0.0.1:8000/api/product/?category=3"
private me="http://127.0.0.1:8000/api/product/?category=2"
getApi():Observable<any>{
return this.http.get (this.URL)
}
component:
ngOnInit() {
this.api.getApi()
.subscribe(
data => this.todo=data
);}
How can I get those URLs inside one Observable
and how to subscribe
them in my component
?
CodePudding user response:
You can use forkJoin
when it comes to having a group of observables
, and you care about the final emitted value of each.
Service:
private URL = "http://127.0.0.1:8000/api/product/?category=3"
private me = "http://127.0.0.1:8000/api/product/?category=2"
getApi(): Observable<any> {
return forkJoin({
data1: this.http.get(this.URL),
data2: this.http.get(this.me)
})
}
Component:
ngOnInit(): void {
this.api.getApi().subscribe(({ data1, data2 }) => {
console.log('response 1', data1);
console.log('response 2', data2);
})
}