In Angular, the ActivatedRouteSnapshot and ActivatedstateSnapshot are used to understand the working and use of a component.
The ActivatedRouteSnapshot is a static image of the route information, while the ActivatedstateSnapshot is a dynamic image of the current state of the route.
By using these two images we can understand the difference between a static and dynamic route.
In this article, we will take a look at the ActivatedRouteSnapshot and ActivatedstateSnapshot classes in Angular. We will see how they are used and what they are for.
By understanding these two classes, we can have a better understanding of how Angular routing works.
When working with the Angular Router, there are two different types of snapshots that can be used:
the ActivatedRouteSnapshot and the ActivatedStateSnapshot. Both of these snapshots provide information about the state of the router at a given moment in time.
However, they differ in how they get their information. The ActivatedRouteSnapshot gets its information from the current route tree, while the ActivatedStateSnapshot gets its information from the current router state.
Learning Objective
- What is ActivatedStateSnapshot.
- What is ActivatedRouteSnapshot.
- ActivatedRoute vs ActivatedRouteSnapshot
- How to get queryParams using ActivatedRouteSnapshot
- Summary
What is ActivatedStateSnapshot.
@Component({templateUrl:'app-template.html'})
class MyComponent {
constructor(router: Router) {
let stateObj: RouterState = router.routerState;
let snapshot: RouterStateSnapshot = stateObj.snapshot;
console.log(snapshot);
}
}
What is ActivatedRouteSnapshot.
@Component({templateUrl:'app-template.html'})
class MyComponent {
constructor(private router: Router) {
let stateObj: RouterState = router.routerState;
let snapshotObj: RouterStateSnapshot = stateObj.snapshot;
let activatedRouteRoot: ActivatedRouteSnapshot = snapshotObj.root;
console.log(activatedRouteRoot);
}
}
ActivatedRoute vs ActivatedRouteSnapshot
- To determine whether 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.
- If you utilize ActivatedRouteSnapshot with a parameter from the time in your route circumstance like product:id, you will not be any newer ID if the user changes them or your site does. Snapshot implies that it was at the time OnInit was executed, which it was at the present time. So, any changes are going to be disregarded.
- ActivatedRouteSnapshot is no mutable data structure.
- Similar to ActivatedRouteSnapshot, ActivatedRoute depicts the state of the router over time.
- In ActivatedRouteSnapshot, every node knows of the consumed URL segments, the parameters, and the resolved information.
- You can use the Snapshot function for initial values into a parameter once only during component initialization, and the URL will not change while the user is on this component.
- You're likely supposed to arrange the subscription, so you'll have to cancel it. You should additionally turn OnDestroy to stop the activated route. There's a lot of overhead for a static route.
How to get queryParams using ActivatedRouteSnapshot
import { Router, RouterStateSnapshot } from '@angular/router';
export class LoginComponent {
constructor(private router: Router) {
const snapshotObj: RouterStateSnapshot = router.routerState.snapshot;
console.log(snapshotObj);
}
}
0 Comments