0

I'm using Angular 8, and I have a guard that opens a page in a separate tab using window.open(url) if a specific condition is met, then returns false because I don't want to leave the current page I am on.

I am doing this logic in the canActivate function:

canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot):
                                      Observable<boolean | UrlTree> | 
                                      Promise<boolean | UrlTree> | boolean | UrlTree {
  if(condition)  
    window.open(url)

  return false;
}

My problem is that canActivate function is being called twice in a row, which causes the new tab to open twice. How can I solve this?

This is how I route to my guard in app.routing.constant

{
    path: dashboardRoutes.MY_ROUTE,
    component: BlankComponent,
    canActivate: [MyGuard]
},
chimera_girl
  • 155
  • 13
  • This was happening to me because it was being called again after a redirect – Jacob Mar 02 '22 at 20:07
  • How did you stop the second call after redirect. For me a page that has guard, if user isn't logged(MSAL popup) it uses router.parseUrl to call the page again. – Sugar Bowl Jan 11 '23 at 23:19

1 Answers1

2

Found the problem, I was routing both in html component by using [routerLink] and in ts by using this.router.navigate, so this func really was called twice. My mistake, hope this can help anyone who has a similar problem

chimera_girl
  • 155
  • 13