Angular Observables - 相关端点
Angular Observables - related endpoints
我有端点,其中 returns 一个端点 url。
http://example.com/get-endpoint
我想调用第一个 GET,然后使用结果 url 调用第二个 GET。
我如何使用链接的可观察对象和 RxJS 来实现这一点?我不知道如何将第一个 GET 的 url 传递给第二个 GET
您可以使用 flatMap
:
this.http.get('http://example.com/get-endpoint')
.pipe(
flatMap(endpoint => this.service.getObservable(result))
)
.subscribe(result => console.log(result));
看看文档here
使用 RxJS 的 switchMap operator.
// your service...
getData(){
return this.httpClient.get('http://example.com/get-endpoint', { responseType: "text"}).pipe(
switchMap(url => this.httpClient.get(url))
);
}
如果你的端点不是return字符串而是JSON,你应该移除{ responseType: "text"}
选项对象,并将右边的属性传递给内部this.httpClient.get
呼叫.
我有端点,其中 returns 一个端点 url。
http://example.com/get-endpoint
我想调用第一个 GET,然后使用结果 url 调用第二个 GET。 我如何使用链接的可观察对象和 RxJS 来实现这一点?我不知道如何将第一个 GET 的 url 传递给第二个 GET
您可以使用 flatMap
:
this.http.get('http://example.com/get-endpoint')
.pipe(
flatMap(endpoint => this.service.getObservable(result))
)
.subscribe(result => console.log(result));
看看文档here
使用 RxJS 的 switchMap operator.
// your service...
getData(){
return this.httpClient.get('http://example.com/get-endpoint', { responseType: "text"}).pipe(
switchMap(url => this.httpClient.get(url))
);
}
如果你的端点不是return字符串而是JSON,你应该移除{ responseType: "text"}
选项对象,并将右边的属性传递给内部this.httpClient.get
呼叫.