簡體   English   中英

可觀察函數未調用

[英]Observable function not getting called

我需要執行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才能致電他們,

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

嘗試這個:

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