Home > database >  what is the difference between this.route.params and this.route.snapshot.params in angular Activated
what is the difference between this.route.params and this.route.snapshot.params in angular Activated

Time:01-21

constructor(private recipeService : RecipeService , private route : ActivatedRoute) { }

  ngOnInit() {
    const id = this.route.params['id'];
const id = this.route.snapshot.params['id'];
  }

what is the difference between route.snapshot.params and route.params in angular when should we use route.snapshot.params and when should we use route.params

CodePudding user response:

As its mentioned on official angular website.

Snapshot: You get the current snapshot of this route at a particular moment in time. (Contains the information about a route associated with a component loaded in an outlet at a particular moment in time. ActivatedRouteSnapshot can also be used to traverse the router state tree.) You can check description with example in the following link. https://angular.io/api/router/ActivatedRouteSnapshot#description

params: Is A Observable and you can subscribe to it. (An observable of the matrix parameters scoped to this route.) You can check description with example in the following link https://angular.io/api/router/ActivatedRoute#description

CodePudding user response:

Angular router "params" is a collection of matrix and query URL parameters, but not really structured.

Snapshot is more powerful and releated the common router.

-> https://angular.io/api/router/ActivatedRouteSnapshot

  •  Tags:  
  • Related