Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular4.x, navigationEnd.url vs navigationEnd.urlAfterRedirects

Tags:

angular

Any explain or doc about the difference between navigationEnd.url and navigationEnd.urlAfterRedirects?

Which one should I use if I want to write a breadcrumb component?

like image 400
slideshowp2 Avatar asked Aug 02 '17 03:08

slideshowp2


People also ask

What is urlAfterRedirects?

My guess is that navigationEnd.url is your current url and navigationEnd.urlAfterRedirects is url that you will be redirected to if you navigate to navigationEnd.url.

What is navigationEnd?

NavigationEndlinkAn event triggered when a navigation ends successfully.

What is NavigationStart in angular?

The Angular Routers triggers several events starting with when the Navigation starts ( NavigationStart ) and also when the Navigation end ( NavigationEnd ) successfully. It is triggered when the navigation is canceled either by the user ( NavigationCancel ) or due to an error in the navigation ( NavigationError).


1 Answers

It depends if you care about the URL that the user requested or the URL of the route that actually gets loaded.

If you define any routes with redirects, the two values will be different if the user navigates to a route that redirects to another route.

For example, take these routes (which components the routes load is not relevant, so they're excluded here):

{
  path: "",
  pathMatch: "full",
  redirectTo: "home"
},
{
  path: "home",
},

If the user navigates to /home, then navigationEnd.url and navigationEnd.urlAfterRedirects will both be /home.

If the user navigates to /, then navigationEnd.url will be / and navigationEnd.urlAfterRedirects will be /home.

Even if you don't implement redirects, try to use the value that is most relevant to the the functionality you are basing on the url because you don't want to run into strange bugs later on if you add redirects.

like image 93
rooby Avatar answered Sep 22 '22 10:09

rooby