Search code examples
angularangular-routing

Angular ActivatedRoute, questo sconosciuto


I have this link: http://localhost:3000/welcome/DqMzM6PNKbQKZ0tPhqkMlgRry1w1/info/anotherid

to take the last one I do so:

console.log(this.ar.root.children[0].children[0].snapshot.params['another']) //anotherid

ok, I have my result but I don't think this is the best paratices, right?

p.s. 'questo sconosciuto' === 'this stranger' ;)


Solution

  • You can get the route parameters multiple ways.

    1. When the component firsts loads.
    2. Subscribing to the Observable and receiving updates when the params change.

    const anotherId = this.ac.snapshot.params.anotherId;
    

    This will give you the params when the component first loads. If the router params change this will not receive the updated values.

    Another option is to subscribe to the params Observable.

    this.ar.params.subscribe(params => {
      this.anotherId = params.anotherId;
    });
    

    If the route params change, the above will be notified of the changed params.

    https://angular.io/guide/router

    https://kamranahmed.info/blog/2018/02/28/dealing-with-route-params-in-angular-5/