【发布时间】:2020-03-19 08:51:05
【问题描述】:
我有一个检索一个用户数据的函数。现在我想像这样在这个函数中获取 user_id:
this.storage.get(USER_ID).then(val => {
this.id = val;
)}
所以 api 知道它需要哪个用户的 id。 我必须插入的主要功能是:
ngOnInit() {
this.subscription = this.authService.authenticationStateSubject.pipe(
switchMap(isAuthenticated => {
if (isAuthenticated) {
return this.userService.getUserDetails(this.id);
} else {
return of(null);
}
}),
).subscribe(
result => {
if (result) {
this.information = result;
console.log(this.information);
} else {
}
},
error => {
}
);
}
我试图将我的 sn-p 放在 if (isAuthenticated) { 之后,但不知何故它不适用于最后两个括号。我真的可以连接这两个sn-ps吗?
组合版
ngOnInit() {
this.subscription = this.authService.authenticationState,
from(this.storage.get(USER_ID))
.pipe(
switchMap(([isAuthenticated, id]) => {
if (isAuthenticated) {
return this.userService.getUserDetails(this.id);
} else {
return of(null);
}
}),
).subscribe(
result => {
if (result) {
this.information = result;
console.log(this.information);
} else {
}
},
error => {
}
);
}
【问题讨论】:
标签: javascript angular typescript ionic-framework