Angular ActivatedRoute,这个未知

Angular ActivatedRoute, questo sconosciuto

我有这个link: http://localhost:3000/welcome/DqMzM6PNKbQKZ0tPhqkMlgRry1w1/info/anotherid

最后一个我这样做:

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

好的,我得到了我的结果,但我认为这不是最好的方法,对吧?

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

您可以通过多种方式获取路由参数。

  1. 组件首次加载时。
  2. 订阅 Observable 并在参数更改时接收更新。

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

这将在组件首次加载时为您提供参数。如果路由器参数发生变化,这将不会收到更新的值。

另一种选择是订阅参数 Observable。

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

如果路由参数发生变化,上面会通知更改的参数。

https://angular.io/guide/router

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