获取TypeError:.finalize不是Rxjs6中的函数

时间:2018-05-29 05:46:58

标签: angular6 rxjs6

在以下函数中,我收到运行时错误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
  }

1 个答案:

答案 0 :(得分:1)

在您的组件中导入此行

从'rxjs /运营商'导入{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
  }