![](/img/trans.png)
[英]RxJS concatMap subscription is called for second Observable separately
[英]concatMap is not subscribing for the second time
我正在实现 concatMap 运算符。 甚至在此之前,我尝试一个接一个地进行两个订阅,并且成功了。 第一次订阅表单的输入字段,第二次订阅 http 请求。 下面是代码:
// AfterViewInit implementation
// this.form is a formGroup with formControl city
ngAfterViewInit() {
this.form.valueChanges.pipe(
map (val=>val.city),
).subscribe(
val => {this.request(val)}
);
}
request(city){
this.httpClient.get(url, {params:{
...
}}).subscribe(
value => {
}, error => {}
);
}
每次更新输入字段时,上面的代码片段都会使 http 调用。 我通过添加 concatMap 对 ngAfterViewInit 进行了如下修改:
ngAfterViewInit() {
this.form.valueChanges.pipe(
map (val=>val.city),
concatMap(val=> this.request(val)),
).subscribe();
}
现在,请求没有按预期发出。 当输入改变时,第一次请求只进行一次。 永远不会为更新的输入字段发出第二个请求。 我在这里做错了什么?
尝试像这样修复请求 function:
request(city){
return this.httpClient.get(url, {params:{
...
}}).pipe(tap(
value => {
}, error => {}
));
}
通过此修复,所有链中将只有一个subscribe
调用,并且操作员将按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.