如何使用 rxjs Observable 更新 Angular Post 请求中 HttpParams 中的值
我在 Angular 13 中有一个轮询服务,它使用 rxjs 间隔从外部服务器提取数据。 我能够在指定的时间间隔内正常获取数据。 问题是我无法在下次调用之前使用一组新数据更新 httpParams 。参数包含一组用作 SQL 约束的 ID。
getAllCurrencies(id, code): Observable<any> {
this.bookingCode = code;
this.bookingId = id;
let url = 'someurl';
let headers = new HttpHeaders({'Content-Type': 'application/x-www-form-urlencoded'});
let body = new HttpParams()
.set('booking_code', this.bookingCode)
.set('booking_id', this.bookingId)
.set('token', this.token);
return this.allCurrencies$ = interval(5000).pipe(
mergeMap(() =>
this.http.post<any>(this.GLOBAL_URL + url, body, { headers: headers }).pipe(
map((res) => {
let result:any = [];
if(res.data.length > 0){
this.testingOne();
this.bookingId = res.data[0].id;
result = res.data;
}
return result;
}))),retry(),share(),takeUntil(this.stopPolling)
);
}
我想在返回新值时更新 .set('booking_id', this.bookingId)
中 this.bookingId
的值,然后继续调用。
I have a polling service in Angular 13 which pulls data from an external server using rxjs interval.
I'm able to get the data alright in the specified interval.
The problem is that I'm unable to update the httpParams with a new set of data before the next call. The params contain a set of IDs used as SQL constrainers.
getAllCurrencies(id, code): Observable<any> {
this.bookingCode = code;
this.bookingId = id;
let url = 'someurl';
let headers = new HttpHeaders({'Content-Type': 'application/x-www-form-urlencoded'});
let body = new HttpParams()
.set('booking_code', this.bookingCode)
.set('booking_id', this.bookingId)
.set('token', this.token);
return this.allCurrencies$ = interval(5000).pipe(
mergeMap(() =>
this.http.post<any>(this.GLOBAL_URL + url, body, { headers: headers }).pipe(
map((res) => {
let result:any = [];
if(res.data.length > 0){
this.testingOne();
this.bookingId = res.data[0].id;
result = res.data;
}
return result;
}))),retry(),share(),takeUntil(this.stopPolling)
);
}
I want to update the value of this.bookingId
in .set('booking_id', this.bookingId)
when a new value is returned and then continue the call.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试像下面这样的东西,它应该可以工作
Try something like below, it should work