简体   繁体   English

concatMap 没有第二次订阅

[英]concatMap is not subscribing for the second time

I am implementing concatMap operator.我正在实现 concatMap 运算符。 Even before doing that I tried making two subscriptions one after the other and it worked.甚至在此之前,我尝试一个接一个地进行两个订阅,并且成功了。 The first subscription is made to the input field of form and the second subscription is made for the http request.第一次订阅表单的输入字段,第二次订阅 http 请求。 Below is the code:下面是代码:

// 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 => {}
    );
  }

The above snippet will make http call for each and every time the input field is updated.每次更新输入字段时,上面的代码片段都会使 http 调用。 I modified the ngAfterViewInit as follows by adding concatMap:我通过添加 concatMap 对 ngAfterViewInit 进行了如下修改:

  ngAfterViewInit() {
    this.form.valueChanges.pipe(
      map (val=>val.city),
      concatMap(val=> this.request(val)),
    ).subscribe();
  }

Now, the requests are not made as expected.现在,请求没有按预期发出。 The request is made only once for the first time when the input is changed.当输入改变时,第一次请求只进行一次。 The second request is never made for the updated input field.永远不会为更新的输入字段发出第二个请求。 What am I doing wrong here?我在这里做错了什么?

try fixing request function like this:尝试像这样修复请求 function:

request(city){
    return this.httpClient.get(url, {params:{
    ...
    }}).pipe(tap(
      value => {

      }, error => {}
    ));
  }

with this fix there will be only one subscribe call in all the chain and operators will work as expected.通过此修复,所有链中将只有一个subscribe调用,并且操作员将按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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