typescript 两个订阅解析后从服务方法返回 Observable<boolean>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46866202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Return Observable<boolean> from service method after two subscriptions resolve
提问by Ben Racicot
I'm trying to setup a simple way to compare the current username with a profile's username within an Angular service.
我正在尝试设置一种简单的方法来将当前用户名与 Angular 服务中的配置文件用户名进行比较。
Obviously the profile username and the user's username must resolve before I can compare them so how do I return a boolean observable so that I can subscribe to this comparison within components?
显然,配置文件用户名和用户的用户名在我可以比较之前必须解析,那么我如何返回一个布尔值 observable 以便我可以在组件内订阅这个比较?
This is where I'm at:
这是我所在的位置:
public profileId = new Subject<string>; // Observable string source updated from a profile.component (when the URL displays the profile's username)
public profileId$ = this.profileId.asObservable();
public currentUser = this.principal.asObservable().distinctUntilChanged();
public isProfileOwner(): Observable<boolean> { // A function whose declared type is neither 'void' nor 'any' must return a value.
this.currentUser.subscribe(user => {
this.profileId$.subscribe(
profile => {
console.log(profile + ' ' + user.username); // match!
if (profile === user.username) {
return Observable.of(true);
} else {
return Observable.of(false);
}
}
)
})
}
This seems to be the way other SO answers explain to do it but I'm getting [ts] A function whose declared type is neither 'void' nor 'any' must return a value.
这似乎是其他 SO 答案解释的方式,但我得到了 [ts] A function whose declared type is neither 'void' nor 'any' must return a value.
I'd like to subscribe to test within components.
我想订阅组件内的测试。
this.authService.isProfileOwner().subscribe(
data => {
console.log(data); // should be boolean
}
)
采纳答案by AJT82
As noticed from other answer by @user184994, forkJoin
won't work in this case. Instead you can use combineLatest
, and then very similarily like @user184994 have otherwise implemented the service code:
正如@user184994 从其他答案中注意到的那样,forkJoin
在这种情况下不起作用。相反,您可以使用combineLatest
, 然后非常相似地像@user184994 以其他方式实现了服务代码:
isProfileOwner(): Observable<boolean> {
return Observable.combineLatest(this.currentUser, this.profileId$)
.map(results => {
let user = results[0];
let profile = results[1];
return (user.username === profile)
});
}
DEMO
演示
回答by Rajani Kanth
This can be achieved through Subject
这可以通过主题来实现
import { Subject } from 'rxjs';
public isProfileOwner(): Observable<boolean> {
var subject = new Subject<boolean>();
this.currentUser.subscribe(user => {
this.profileId$.subscribe(
profile => {
console.log(profile + ' ' + user.username); // match!
if (profile === user.username) {
subject.next(true);
} else {
subject.next(false);
}
}
)
})
return subject.asObservable();
}
回答by user184994
I would personally suggest using forkJoin
, to wait for the observables, and flatMap to convert to an Observable<boolean>
我个人建议使用forkJoin
, 等待 observables 和 flatMap 转换为Observable<boolean>
return Observable.forkJoin(this.currentUser, this.profileId$).flatMap(
results => {
user = results[0];
profile = results[1];
return Observable.of(profile === user.username)
}
);