【发布时间】:2018-05-16 15:34:49
【问题描述】:
在使用管道和地图时如何返回值,例如在这种情况下:
return this.service.someEvent().pipe(
map(x => {
return this.service.someValue;
})
)
但该值永远不会返回,因为从未订阅过该事件,如果我这样使用它:
const a = this.service.someEvent().pipe(
map(x => {
return this.service.someValue;
})
)
return a.subscribe();
我得到一个错误(我知道这是错误的)。
TS2322: Type 'Subscription' is not assignable to type 'boolean | Observable<boolean> | Promise<boolean>'. Type 'Subscription' is not assignable to type 'Promise<boolean>'. Property 'then' is missing in type 'Subscription'.
所以我需要 i 方法来返回值。
更新: 更好的描述(我需要用最新版本的 rxjs 复制这段代码):
canActivate(next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
return this.service.something().map(x => {
if (x) {
return true;
} else {
return false;
}
})
}
需要在管道内使用地图(据我了解)。
【问题讨论】:
-
这看起来像你试图在一个方法中调用
return a.subscribe();,其类型表明它应该返回Observable。函数subscribe()返回Subscription实例
标签: javascript angular rxjs reactivex