[英]How to return null in the function returning Observable object
Below method return object of user in first return, in outside return unable to return null.下面方法先返回用户的object,在外返回无法返回null。
login (username:any, password:any):Observable<User> {
const credentials={
"userName":username,
"password":password
}
this.http.post<User>("https://localhost:5001/api/Person/login",credentials)
.pipe(map(user=>{
localStorage.setItem('user',JSON.stringify(user));
this.userSubject.next(user);
return user;
}));
return null;
}
even I tried Observable<User|null>
its not allow to return null type.即使我尝试了
Observable<User|null>
它也不允许返回 null 类型。
login (username:any, password:any):Observable<User> | null {
But what you're doing doesn't make sense.但你在做什么没有意义。 You should return the observable or make the entire method void.
您应该返回 observable 或使整个方法无效。 My guess what you're looking for is
我猜你在找什么
login (username:any, password:any) {
const credentials={
"userName":username,
"password":password
}
this.http.post<User>("https://localhost:5001/api/Person/login", credentials)
.subscribe(user => {
localStorage.setItem('user', JSON.stringify(user));
this.userSubject.next(user);
}));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.