【发布时间】:2022-06-23 09:23:54
【问题描述】:
在我的 Angular-13 项目中,我有这个模型界面:
export interface IUser {
email: string;
token: string;
}
还有这项服务:
export class AccountService {
baseUrl = environment.apiUrl;
private currentUserSource = new ReplaySubject(1);
currentUser$ = this.currentUserSource.asObservable();
constructor(private http: HttpClient, private router: Router) { }
loadCurrentUser(token: string) {
if (token === null) {
this.currentUserSource.next(null);
return of(null);
}
let headers = new HttpHeaders();
headers = headers.set('Authorization', `bearer ${token}`);
return this.http.get(this.baseUrl + 'account', {headers}).pipe(
map((user: IUser) => {
if (user) {
localStorage.setItem('token', user.token);
this.currentUserSource.next(user);
}
})
);
}
}
但是,我收到了这个错误:
null 类型的参数不能分配给 IUser 类型的参数
null
突出显示在:
this.currentUserSource.next(null);
我该如何解决这个问题?
谢谢。