使用Angular 2 Router(版本3)导航相对 [英] Navigate relative with Angular 2 Router (Version 3)
问题描述
如何在不使用字符串连接并指定/questions/123/
的情况下使用Router
在组件内从/questions/123/step1
导航至/questions/123/step2
?
How to navigate relative from /questions/123/step1
to /questions/123/step2
within a component using Router
without string concatenation and specifying /questions/123/
?
我在下面添加了自己的答案.请随时提出更好的答案. ;-)我猜有更好的方法.
I've added an own answer below. Please feel free to suggest a better answer. ;-) I guess there are better approaches.
推荐答案
进行了更多研究之后,我发现了
After doing some more research I came across with
this.router.createUrlTree(['../step2'], {relativeTo: this.activatedRoute});
和
this.router.navigate(['../step2'], {relativeTo: this.activatedRoute});
第一种方法(路由器.createUrlTree API )对我不起作用,即什么也没发生.第二种方法( Router.navigate API ).
First approach (Router.createUrlTree API) did not work for me, i.e. nothing happened. Second approach (Router.navigate API) works.
但是,第二种方法使用NavigationExtras
(第二个参数),该文档记录在@experimental
中.希望下一个版本不会有太大变化... NavigationExtras
将保持稳定.
However, second approach uses NavigationExtras
(second argument) which is documented with @experimental
. Hopefully there won't be big changes with the next release again... and NavigationExtras
will be stable.
任何其他建议/方法,请随时回答我上面的问题.
Any other suggestions/approaches, please do not hesitate to answer to my question above.
更新2016-10-12
还有另一个stackoverflow问题:
There is another stackoverflow question as well:
更新2016-10-24
文档:
更新2019-03-08
似乎Angular 7.1有所更改.在另一篇文章中有一个答案如何使用Angular 7.1来解决.请参阅 https://stackoverflow.com/a/38634440/42659 .
It seems that there were changes in Angular 7.1. There is an answer in another post how to solve it with Angular 7.1. Please refer to https://stackoverflow.com/a/38634440/42659.
这篇关于使用Angular 2 Router(版本3)导航相对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!