Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between ActivatedRoute and ActivatedRouteSnapshot in Angular4

What is the difference between ActivatedRouteSnapshot and ActivatedRoute in Angular 4? It's my understanding that ActivatedRouteSnapshot is a child of ActivatedRoute, meaning that ActivatedRoute contains ActivatedRouteSnapshot.

Incidentally, I tried running a Google search for an answer to this question, but I didn't find any of the search results to be understandable.

Thank you!

like image 246
RajnishCoder Avatar asked Sep 05 '17 08:09

RajnishCoder


People also ask

What is the difference between routes router and ActivatedRoute?

Difference between activatedroute and routerstateRouterState is an interface which represents the state of the router as a tree of activated routes. Every node of this tree knows about the "consumed" URL segments, the extracted parameters, and the resolved data.

What is ActivatedRoute?

The ActivatedRoute is the API that holds observable data that you can use to react to events with. You can subscribe to specific observable properties to get data from it asynchronously. The ActivatedRoute also contains ActivatedRouteSnapshot.

Why do we use ActivatedRoute in Angular?

ActivatedRoutelink. Provides access to information about a route associated with a component that is loaded in an outlet. Use to traverse the RouterState tree and extract information from nodes.

How do I get the current URL in ActivatedRoute?

You can use the activated route to get active URL. (<RouterStateSnapshot>activatedRoute['_routerState']). url , if you like to type it.


2 Answers

Since ActivatedRoute can be reused, ActivatedRouteSnapshot is an immutable object representing a particular version of ActivatedRoute. It exposes all the same properties as ActivatedRoute as plain values, while ActivatedRoute exposes them as observables.

Here is the comment in the implementation:

export class ActivatedRoute {   /** The current snapshot of this route */   snapshot: ActivatedRouteSnapshot; 

If a router reuses a component and doesn't create a new activated route, you will have two versions of ActivatedRouteSnapshot for the same ActivatedRoute. Suppose you have the following routing configuration:

path: /segment1/:id, component: AComponent 

Now you navigate to:

/segment1/1 

You will have the param in the activatedRoute.snapshot.params.id as 1.

Now you navigate to:

/segment1/2 

You will have the param in the activatedRoute.snapshot.params.id as 2.

You can see it by implementing the following:

export class AComponent {   constructor(r: ActivatedRoute) {         r.url.subscribe((u) => {       console.log(r.snapshot.params.id);     }); 
like image 139
Max Koretskyi Avatar answered Oct 19 '22 21:10

Max Koretskyi


There are 2 ways to get the parameter from the route.

1. Snapshot (route.snapshot.paramMap.get). Read it during init.

Use the Snapshot if you only need the initial value of the parameter once during the component's initialization, and don't expect the URL to change while the user is still on that same component.

  • I.e. if on a product/2 route, and the only way they'd get to product/3 is by going back to the product search screen and then clicking a product detail (leaving the detail component, then re-opening it with a new route param)

2. Observable (route.paramMap.subscribe). Subscribe to it during init.

Use the Observable if it's possible for the route to change while the user is still on the same component, and hence the Component's initialization would not be called again, but the observable would call your subscribed logic when the URL changed.

  • I.e. if on a product/2 route, and you have a "next" button to go to the next id record product/3, hence the user did not leave/re-open the component but the URL did receive a new param.

Generally speaking, subscribing is the safest route if you're unsure.

like image 37
Don Cheadle Avatar answered Oct 19 '22 23:10

Don Cheadle