1

在以下函数中,我收到运行时错误TypeError: this.authService.authServiceSigninUser(...).map(...).catch(...).finalize is not a function

问题 1) 为什么 Typescript 在编译时没有捕捉到这个错误?我怎么能在编译时捕捉到这个错误。

问题 2) 我该如何解决这个错误?我尝试使用两者finallyfinalize但两者都不起作用。

public signinUser(user:UserSigninInfo):any{

    this.loaderService.show(); //this shows a spinner
    return this.authService.authServiceSigninUser(user)
      .map(response=>{
        console.log('response from backend service',response);
        let result= <HttpResponse<any>>response; 
        let authToken = result.headers.get("x-auth-token")
        if(authToken == null) {
          console.log("no authToken")
        } else {
          console.log("authToken is ",authToken)
          localStorage.setItem('id_token', authToken);
        }
        return result;
      })
      .catch(this.handleError)
    .finally(()=> this.loaderService.hide()) //hide the spinner
  }
4

1 回答 1

3

在您的组件中导入这一行

从 'rxjs/operators' 导入 {finalize,catchError,tap};

试试这个代码

 public signinUser(user:UserSigninInfo):any{

    this.loaderService.show(); //this shows a spinner
    return this.authService.authServiceSigninUser(user)
      .pipe(tap(response=>{
        console.log('response from backend service',response);
        let result= <HttpResponse<any>>response; 
        let authToken = result.headers.get("x-auth-token")
        if(authToken == null) {
          console.log("no authToken")
        } else {
          console.log("authToken is ",authToken)
          localStorage.setItem('id_token', authToken);
        }
        return result;
      })
      ,catchError(this.handleError)
    ,finalize (()=> this.loaderService.hide())); //hide the spinner
  }
于 2018-05-29T08:59:33.407 回答