【问题标题】:Type 'Observable<ArrayBuffer>' is not assignable to type 'Observable<HttpResponse<User>>'类型 'Observable<ArrayBuffer>' 不可分配给类型 'Observable<HttpResponse<User>>'
【发布时间】:2022-01-08 12:22:01
【问题描述】:

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

类型 'Observable' 不可分配给类型 '可观察的'。类型“ArrayBuffer”缺少 来自“HttpResponse”类型的以下属性:body、type、 克隆、标题和另外 4 个。

功能如下:

  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;
    }
  ))
}

我该如何解决这个问题?

【问题讨论】:

    标签: angular typescript api authentication rxjs


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢你这个作品,我应该注意区分大小写。
    猜你喜欢
    • 2022-06-25
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 2018-08-14
    • 2021-01-02
    • 2020-11-23
    相关资源
    最近更新 更多