Angular 可观察的回报<;布尔值>;解析两个订阅后从服务方法

Angular 可观察的回报<;布尔值>;解析两个订阅后从服务方法,angular,typescript,rxjs,rxjs5,Angular,Typescript,Rxjs,Rxjs5,我正在尝试设置一种简单的方法来比较Angular服务中当前用户名和配置文件的用户名 显然,在比较配置文件用户名和用户用户名之前,必须先解析它们,那么如何返回布尔可观测值,以便在组件中订阅此比较 这就是我所处的位置: public profileId = new Subject<string>; // Observable string source updated from a profile.component (when the URL displays the profile'

我正在尝试设置一种简单的方法来比较Angular服务中当前用户名和配置文件的用户名

显然,在比较配置文件用户名和用户用户名之前,必须先解析它们,那么如何返回布尔可观测值,以便在组件中订阅此比较

这就是我所处的位置:

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);
                    }
                }
            )
        })
}

这可以通过主题来实现

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();
    }
从'rxjs'导入{Subject};
公共isProfileOwner():可观察{
var subject=新的subject();
this.currentUser.subscribe(用户=>{
此.profileId$.subscribe(
个人资料=>{
console.log(profile+''+user.username);//匹配!
if(profile==user.username){
subject.next(true);
}否则{
主题。下一个(假);
}
}
)
})
返回subject.asObservable();
}

我个人建议使用
forkJoin
,等待可观察的对象,并使用flatMap将其转换为可观察的


从@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)
    });
}
isProfileOwner():可观察{
返回Observable.CombineTest(this.currentUser,this.profileId$)
.map(结果=>{
让用户=结果[0];
let profile=结果[1];
返回(user.username==配置文件)
});
}

回答得很好。你能解释我应该如何订阅这个布尔值吗?(获得空白结果)你说的“空白结果”是什么意思?我已经更新了问题以显示我的订阅代码。记录结果时不会显示任何内容。如中所示,从未打印日志消息?很好。但是我认为你可以使用map()iso flatmap()?这太棒了,我已经更新了这个问题!我注意到它对我不起作用,因为我最初的
profileId
主题
源代码。我需要这是一个主题(我想),以便profile.component可以用实际的profile用户名更新它。然后成为一个流:
profileId$=this.profileId.asObservable()主体和行为主体的区别在于主体需要
next
订阅fire。不知道您的完整用例,但我不明白为什么您不能将其作为初始值为
null
的行为主体,然后执行
null
检查?你有可能这么做吗?谢谢你的耐心,我更新行为主题时出错了。很好地回答了一个复杂的问题。没问题,很高兴我能帮忙!user184994也做得很好,只需要一些小的调整就可以了,很高兴我们为您找到了解决方案!:)
return Observable.forkJoin(this.currentUser, this.profileId$).flatMap(
    results => {
        user = results[0];
        profile = results[1];
        return Observable.of(profile === user.username)
    }
);
isProfileOwner(): Observable<boolean> {
  return Observable.combineLatest(this.currentUser, this.profileId$)
    .map(results => {
       let user = results[0];
       let profile = results[1];
       return (user.username === profile)
    });
}