晚上好,我正在嘗試設定身份驗證服務,但登錄功能一直顯示此錯誤:
“Observable”型別不能分配給“Observable<HttpResponse>”型別。型別 'ArrayBuffer' 缺少型別 'HttpResponse' 中的以下屬性:主體、型別、克隆、標題等 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;
}
))
}
我該如何解決這個問題?
uj5u.com熱心網友回復:
您實際上現在正在使用 fetch api 介面Response,這就是您收到此錯誤的原因。該observe選項'body','events'或'response'。注意r回應中的小寫字母,它也是一個字串。話雖如此,您的代碼應該是:
return this.http.post<User>(`${this.apiUrl}/login`, user, {observe: 'response'});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/372868.html
