简体   繁体   English

可观察函数未调用

[英]Observable function not getting called

I need to execute getData() and then proceed with another call getOtherData But I have an issue of my getData() not getting executed. 我需要执行getData() ,然后继续进行另一个调用getOtherData但是我有一个getData()无法执行的问题。 Also I need to return data from this.service.getOtherData() so I can't use .subscribe() Here is how I composed my operators. 另外我还需要从this.service.getOtherData()返回数据,所以我不能使用.subscribe()这就是我组成运算符的方式。

My first call I need to make: 我的第一个电话需要拨打:

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

Does not work: 不起作用:

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

Any thoughts how to fix? 有什么想法如何解决?

You need to call .subscribe inorder to get them called, 您需要致电.subscribe才能致电他们,

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

Try this: 尝试这个:

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

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

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