Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular canActivate changes URL

I have implemented the CanActivate router guard for my Angular 5.2 app. If it returns true the URL does not change, but when it returns false, the URL changes and appends the path to the URL. Everywhere in the app that we change the route it skipsLocationChange so the URL never changes, but it does in this case. This is what the canActivate code looks like basically.

canActivate(){
  if(someRightsThatReturnsTrue)
    return true;
  else{
    this.router.navigateByURL('noauthpage', {skipLocationChange: true});
    return false;
  }
}

Any ideas on why the URL is changing? Is it an Router mechanic because it can't render the page and is 'failing'?

like image 413
Nathaniel Gottschalt Avatar asked Dec 21 '25 00:12

Nathaniel Gottschalt


1 Answers

found a workaround with it, once called the

this.router.navigate(['noauthpage'], { skipLocationChange: true });

you may call

this.location.replaceState('current_page');

to replace the browser url immediately.

like image 191
Ali Avatar answered Dec 22 '25 15:12

Ali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!