简体   繁体   中英

Angular service to return true or false if the route has queryParams ¿How to handle the subscription?

I'm creating a guard in Angular that have to return true or false if the route has query params or not.

I've create a service like this that return an Observable true or false:

export class MatchService {
  constructor(private route: ActivatedRoute) {}
  validateQuerys(): Observable<boolean> {
    return this.route.queryParams.pipe(
      map(res => {
        return res['name'] ? true:false;
      })
    )
  }
}

And now the problem, how to handle it in my canMatch guard?

export class routeMatcher implements CanMatch {

  constructor(private matchService: MatchService) {}

  canMatch(): boolean {

    this.matchService.validateQuerys().subscribe({
      next: res => (console.log(res))
      // return?
      // ¿How I can return true or false?
    })

   }
}

The problem is that I do not know where to place the return true or false in the canMatch() function.

Ignoring my comment, your solution is as simple as that.

return this.matchService.validateQuerys().pipe(first());

Delete your return type on your function to avoid typing errors, because anyways, the function type is inferred from the parent

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM