4

I am in Authenticate with authGuard. I would like to know the difference between these two

// Redirect to the login page

return router.navigate(['/login']);
return router.parseUrl('/login');

In Angular official documentation it is showing router.parseUrl('/login'); is used for redirecting to login page https://angular.io/guide/router-tutorial-toh#canactivate-requiring-authentication

enter image description here

But in other online tutorial website, they are using return router.navigate(['/login']);

What are the difference between these two?

1 Answer 1

1

"The main advantage is prioritization. With the old API, there was no way for prioritizing redirects. Assume you’re having multiple guards active doing async validation and redirects on failure. It was purely dependent on the execution time of those async operations: whichever async guard invoked a redirect as last was the one to win. By now returning an UrlTree rather than running the redirect directly, Angular can take care of executing it and make sure to properly prioritize multiple route guards potentially running. The prioritization works by giving the guard closest to the root of the application the highest priority. As a result, if a child guard returns false or an UrlTree but its parent hasn’t resolved yet, it’ll wait until the parent resolves. If the parent guard check fails, it’ll take priority over all the others.

Cancellation Another benefit is the possibility to cancel all other navigation events. Whenever a UrlTree is returned from a route guard, Angular fires a NavigationCancel event, thus effectively cancelling all running navigation events and kicking off a new navigation to the indicated URL."

Full Article: https://juri.dev/blog/2018/11/better-route-guard-redirects/

Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.