Reputation: 3735
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!
Upvotes: 105
Views: 92569
Reputation: 2661
gives updated data when different page is hit (snapshot & parameters changes)
gives previous data when same page is hit again with different parameters when same page is hot again with different parameters (snapshot does not change)
By using the snapshot property of the ActivatedRoute
gives updated data whether different page is hit or same page is hit again with different parameters
By suscribing to the param property of the ActivatedRoute
Upvotes: 1
Reputation: 2638
One of the key differences not highlighted by the other answers here is the fact that ActivatedRoute
can be injected into a component, while ActivatedRouteSnapshot
cannot.
As mentioned in this answer, you access ActivatedRouteSnapshot
in a component by injecting ActivatedRoute
, and then accessing its snapshot
property, like so:
constructor(route: ActivatedRoute) {
let activatedRouteSnapshot = route.snapshot;
}
On the other hand, trying to inject ActivatedRouteSnapshot
directly into the component will result in an error like this:
ERROR Error: Uncaught (in promise): Error: StaticInjectorError(AppModule)[YourComponent -> ActivatedRouteSnapshot]: StaticInjectorError(Platform: core)[YourComponent -> ActivatedRouteSnapshot]: NullInjectorError: No provider for ActivatedRouteSnapshot!
See also the documentation for ActivatedRoute and ActivatedRouteSnapshot
Upvotes: 23
Reputation: 5586
There are 2 ways to get the parameter from the route.
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.
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.
Generally speaking, subscribing is the safest route if you're unsure.
Upvotes: 67
Reputation: 105547
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);
});
Upvotes: 101