Angular ActivatedRoute,问题解答

时间:2019-03-06 22:30:59

标签: angular angular-routing

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

我要接最后一个:

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

好的,我有我的结果,但是我不认为这是最好的选择,对吗?

p.s。 'questo sconosciuto'==='这个陌生人';)

1 个答案:

答案 0 :(得分:1)

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

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

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

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

另一种选择是订阅可观察的参数。

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/