Angular - 'OperatorFunction<IUser, void>' 类型的参数不能分配给 'OperatorFunction<Object, void>' 类型的参数
Angular - Argument of type 'OperatorFunction<IUser, void>' is not assignable to parameter of type 'OperatorFunction<Object, void>'
当我尝试在 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);
}
})
);
}
}
我在尝试实施时遇到此错误:
Argument of type 'OperatorFunction<IUser, void>' is not assignable to parameter of type 'OperatorFunction<Object, void>'
这是突出显示的:
map((user: IUser) => {
我该如何解决这个问题?
谢谢
如您所料,您返回的响应是 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) => {
...
})
);
}
参考资料
当我尝试在 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);
}
})
);
}
}
我在尝试实施时遇到此错误:
Argument of type 'OperatorFunction<IUser, void>' is not assignable to parameter of type 'OperatorFunction<Object, void>'
这是突出显示的:
map((user: IUser) => {
我该如何解决这个问题?
谢谢
如您所料,您返回的响应是 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) => {
...
})
);
}