【问题标题】:Observable function not getting called可观察的函数没有被调用
【发布时间】:2018-05-24 11:33:37
【问题描述】:

我需要执行getData() 然后继续另一个调用getOtherData 但是我的getData() 没有被执行的问题。另外我需要从this.service.getOtherData() 返回数据,所以我不能使用.subscribe() 这是我如何组成我的运算符。

我需要打的第一个电话:

public getData(): Observable<string> {
    return this.http.get<string>(apiUrl)
}

不起作用:

myFunction() {
    this.service.getData().pipe(
        tap(data => {
            doStuff(data); // not getting here
        }),
        switchMap(() => {
            return this.service.getOtherData().pipe(
                tap((data) => {
                    sc.dispatch(new DoSomethingElse(data));
                }),
                catchError(err => of(tap(() => {})))
            );
        })
    );
}

任何想法如何解决?

【问题讨论】:

  • 使用 .subscribe 代替 .pipe
  • @RobertChan @NitinSingh 我需要从this.service.getOtherData()返回数据

标签: angular rxjs observable


【解决方案1】:

您需要调用 .subscribe 才能调用它们,

this.dataService.getData()
      .pipe(
        tap(console.log),
        tap(data => doStuff(data)))
      ).subscribe();

【讨论】:

    【解决方案2】:

    试试这个:

    public getData(): Observable<string> {
        return this.http.get<string>(apiUrl).subscribe();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-16
      • 2020-01-01
      • 2017-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-26
      相关资源
      最近更新 更多