本文介绍了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路由器-使用不同的参数导航到当前页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!