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

Viewed 20

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.

1 Answers

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

Related