Angular 2 redirect on page load

1.6k views Asked by At

In my Angular 2 app i would like to have a redirect to a certain page if a cookie is set. The behavior should be:

  • No cookie -> Normal routing
  • Cookie is set -> Redirect to the saved route which was set within the cookie

What may be the best practice to do this? My idea is to use ngOnInit() within root component AppComponent, then check if a cookie is set and maybe redirect if the current url is not the same from the cookie.

Is this a possible solution?

My first problem was that this.router.url is always "/" within AppComponent.

Also navigating within ngOnInit() in AppComponent doesnt work: this.router.navigate(['/stepX']);

0

There are 0 answers