类型 'Observable<ArrayBuffer>' 不可分配给类型 'Observable<HttpResponse<User>>'

Type 'Observable<ArrayBuffer>' is not assignable to type 'Observable<HttpResponse<User>>'

晚上好,我正在尝试设置身份验证服务,但登录功能一直显示此错误:

Type 'Observable' is not assignable to type 'Observable<HttpResponse>'. Type 'ArrayBuffer' is missing the following properties from type 'HttpResponse': body, type, clone, headers, and 4 more.

函数如下:

  login(user:User): Observable<HttpResponse<User>>{
    return this.http.post<User>(`${this.apiUrl}/login`, user, {observe: Response});
  }

用户模型界面是:

export interface User {
    username: string;
    password:string;
}

我调用这个函数是我的登录组件:

onLogin(user: User):void{
  this.subs.add(
  this.authService.login(user).subscribe(
    (response) =>{
      this.authService.addTokenToCache(response.headers.get('Jwt-Token') || '{}');
      // this.authService.addUserToCache(response.body|| '{}');
      this.router.navigateByUrl("/home");
      this.showLoading=false;
    },
    (error: HttpErrorResponse)=>{
      alert(error.message);
      this.showLoading=false;
    }
  ))
}

我该如何解决这个问题?

您实际上正在使用获取 api 接口 Response,这就是您收到此错误的原因。 observe 选项为 'body''events''response'。请注意响应中的小写 r 并且它也是一个字符串。也就是说,您的代码应该是:

return this.http.post<User>(`${this.apiUrl}/login`, user, {observe: 'response'});