繁体   English   中英

Angular - '(error: any) => void' 类型的参数不可分配给'() => void' 类型的参数

[英]Angular - Argument of type '(error: any) => void' is not assignable to parameter of type '() => void'

我在 Angular-11 中得到了这个错误处理程序代码:

    return this.api.post('auth/user/login', data, headers).subscribe(
      (result: any) => {
        console.log(result.message);
        console.log(result.code);
        if (result.error === 'true' ){
          console.log(result.message);
        }
      },            
          data => this.tokenHandler(data),
          error => this.errorHandler(error.error)
     );
    }


    errorHandler(error: any){
      this.notify.clear();
    //  console.log(error);
      if (error.errors && error.errors.username){
        this.error = error.errors.username;
      }
      else if (error.message === 'Unauthorized'){
        this.error = null;
        this.notify.error('Invalid Login Details or email not confirmed', {timeout: 0})
      } else {
        this.error = null;
        this.notify.error(error.message, {timeout: 0})
      }
    }

但它导致了这些错误:

 Error: src / app / auth.component.ts: 84: 11 - error TS2769: No overload matches this call. The last overload gave the following error. Argument of type '(error: any) => void' is not assignable to parameter of type '() => void'. 84 error => this.errorHandler(error.error) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ node_modules / rxjs / internal / Observable.d.ts: 54: 5 54 subscribe(next? : (value: T) => void, error? : (error: any) => void, complete? : () => void): Subscription; ~~~~~~~~~ The last overload is declared here. Error: src / app / auth.component.ts: 84: 11 - error TS7006: Parameter 'error' implicitly has an 'any' type. 84 error => this.errorHandler(error.error)

此行突出显示: error => this.errorHandler(error.error)

我该如何解决?

谢谢

这解释了它subscribe(next? : (value: T) => void, error? : (error: any) => void, complete? : () => void): Subscription;

您正在传递三个 arguments (回调)来subscribe ,是的。 但是,您的第三个回调(错误处理程序)与上述 function 定义的第三个参数不匹配。 它接受不带参数的回调作为第三个参数。

相反,看起来第一个回调应该从结果中获取令牌( result.data ?)并在那里调用this.tokenHandler ,第二个回调应该是您的错误处理程序。 基本上摆脱data => this.tokenHandler(data),

在我看来, errorHandler在没有参数的情况下被调用。 也许传递给它的错误没有error属性? (意思是error.error未定义?)

暂无
暂无

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

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