Angular2 路由器 - 使用不同的参数导航到当前页面 [英] Angular2 router - Navigate to the current page with different parameters

查看:30
本文介绍了Angular2 路由器 - 使用不同的参数导航到当前页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用不同的参数路由到当前页面,但无济于事.我有一个触发组合框:

I am trying to route to the current page with different param with no avail. I have a combo box that triggers:

this.router.navigate(['page', selectedId]);

网址正在改变,但仅此而已.

the Url is changing but that's about it.

如何路由到具有不同参数的同一页面?

How do I route to the same page with different param?

推荐答案

页面不会刷新,因为 Router 就是这样工作的.. 没有刷新页面!

The page will not refresh, cause that's how the Router works.. without refreshing the page!

你必须订阅路由参数来检测它们的变化,然后对这些信息做任何你想做的事情..

You have to subscribe to the route parameters to detect changes in them and then do whatever you want to with that information..

因此将 ActivatedRoute 注入您的组件并订阅如下参数:

So inject ActivatedRoute into your component and subscribe to the parameters like this:

constructor(private route: ActivatedRoute) {}

ngOnInit() {
  this.route.params.subscribe(params => {
     // PARAMS CHANGED .. TO SOMETHING REALLY COOL HERE ..

     // for example extract the id..
     let id = +params['id']; // (+) converts string 'id' to a number

   });
}

这篇关于Angular2 路由器 - 使用不同的参数导航到当前页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆