javascript - rxjs: returning value with pipe(map) - Stack Overflow

How to return value when using pipe and map, like in this scenario:return this.service.someEvent().pipe

How to return value when using pipe and map, like in this scenario:

return this.service.someEvent().pipe(
  map(x => {
    return this.service.someValue;
  })
)

but the value is never returned since never subscrubed to the event, if i use it like this:

const a = this.service.someEvent().pipe(
  map(x => {
    return this.service.someValue;
  })
)

return a.subscribe();

i get an error (i know this is wrong).

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'.

So i need i way to return the value.

UPDATE: the better description (i need to duplicate this code with the latest version of rxjs):

canActivate(next: ActivatedRouteSnapshot,
               state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
  return this.service.something().map(x => {
    if (x) {
      return true;
    } else {
      return false;
    }
  })

}

need to do it with map inside pipe (as i understood).

How to return value when using pipe and map, like in this scenario:

return this.service.someEvent().pipe(
  map(x => {
    return this.service.someValue;
  })
)

but the value is never returned since never subscrubed to the event, if i use it like this:

const a = this.service.someEvent().pipe(
  map(x => {
    return this.service.someValue;
  })
)

return a.subscribe();

i get an error (i know this is wrong).

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'.

So i need i way to return the value.

UPDATE: the better description (i need to duplicate this code with the latest version of rxjs):

canActivate(next: ActivatedRouteSnapshot,
               state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
  return this.service.something().map(x => {
    if (x) {
      return true;
    } else {
      return false;
    }
  })

}

need to do it with map inside pipe (as i understood).

Share Improve this question edited May 16, 2018 at 17:02 Igor asked May 16, 2018 at 15:34 IgorIgor 2772 gold badges7 silver badges16 bronze badges 1
  • 1 This looks like you're trying to call return a.subscribe(); in a method whose typings say it should return Observable. Function subscribe() returns Subscription instance – martin Commented May 16, 2018 at 15:38
Add a ment  | 

1 Answer 1

Reset to default 3

You can simply return the observable itself from the "canActivate" guard :-

canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
  return this.service.something().pipe(map(x => {
    if (x) {
      return true;
    } else {
      return false;
    }
  }));
}

Routing will proceed only after the returned observable is plete and its emitted value is true. So you don't actually have to worry about subscribing to that observable angular router will handle it of its own.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744052547a4550336.html

相关推荐

  • javascript - rxjs: returning value with pipe(map) - Stack Overflow

    How to return value when using pipe and map, like in this scenario:return this.service.someEvent().pipe

    9天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信