簡體   English   中英

角度6-永不拋出

[英]Angular 6 - Observable never with throw

我正在尋找處理Angular中可觀察物的最佳方法。 當前的設計是:

public login(email: string, password: string): Observable<string> {
    return this.http
      .post(environment.apiUrl + '/login', {
        email: email,
        password: password
      })
      .map(response => {
        this.authenticationService.setToken(response["token"]);
        return "OK;"
      })
      .catch(error => {
        console.error('LoginService::error', error);
        return throwError(error);
      });
  }

一切正常,但是我不想無故返回“ OK”。

我嘗試了以下操作,但它說您不能將Observable <void>分配給Observable <never>

public login(email: string, password: string): Observable<never> {
    return this.http
      .post(environment.apiUrl + '/login', {
        email: email,
        password: password
      })
      .map(response => {
        this.authenticationService.setToken(response["token"]);
      })
      .catch(error => {
        console.error('LoginService::error', error);
        return throwError(error);
      });
  }
  public login(email: string, password: string) {
    return this.http
      .post(environment.apiUrl + '/login', {
        email: email,
        password: password
      }).pipe(
        tap({
          next: response => {
            this.authenticationService.setToken(response["token"]);
          }, 
          error: error => {
            console.error('LoginService::error', error);
          },
        }),
      )
  }

抽頭運算符會根據需要為每次發射以及錯誤/完成事件在Observable中添加一些邏輯。 運算符不期望任何返回值,並且完全忽略它們,並且與Observable一起發生的所有其他事件將照常繼續。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM