當我嘗試在 Angular-13 中創建用戶登錄時,我有這個模型:
export interface IUser {
email: string;
token: string;
}
服務:
export class AccountService {
baseUrl = environment.apiUrl;
private currentUserSource = new ReplaySubject<IUser>(1);
currentUser$ = this.currentUserSource.asObservable();
constructor(private http: HttpClient, private router: Router) { }
login(values: any) {
return this.http.post(this.baseUrl 'account/login', values).pipe(
map((user: IUser) => {
if (user) {
localStorage.setItem('token', user.token);
this.currentUserSource.next(user);
}
})
);
}
}
我在嘗試實作它時遇到了這個錯誤:
“OperatorFunction<IUser, void>”型別的引數不可分配給“OperatorFunction<Object, void>”型別的引數
這是突出顯示的:
地圖((用戶:IUser)=> {
我該如何解決?
謝謝
uj5u.com熱心網友回復:
正如您期望回傳的回應是IUser型別一樣,然后指定T為IUser型別。
post<T>(url: string, body: any, options?: { headers?: HttpHeaders | { [header: string]: string | string[]; }; context?: HttpContext; observe?: "body"; params?: HttpParams | { [param: string]: string | number | boolean | ReadonlyArray<string | number | boolean>; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }): Observable<T>
login(values: any) {
return this.http.post<IUser>(this.baseUrl 'account/login', values).pipe(
map((user: IUser) => {
...
})
);
}
StackBlitz 上的示例演示
參考
HttpClient post() 多載 #15
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/392339.html
標籤:有角的 打字稿 角-httpclient
