【发布时间】:2018-04-02 15:13:58
【问题描述】:
我正在尝试设置一种简单的方法来将当前用户名与 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);
}
}
)
})
}
这似乎是其他 SO 答案的解释方式,但我收到了 [ts] A function whose declared type is neither 'void' nor 'any' must return a value.
我想订阅组件内的测试。
this.authService.isProfileOwner().subscribe(
data => {
console.log(data); // should be boolean
}
)
【问题讨论】:
标签: angular typescript rxjs rxjs5