使用 Angular 2 路由器(版本 3)进行相对导航 [英] Navigate relative with Angular 2 Router (Version 3)

查看:20
本文介绍了使用 Angular 2 路由器(版本 3)进行相对导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Router 在组件内从 /questions/123/step1 相对导航到 /questions/123/step2 而无需字符串连接并指定 /questions/123/?

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});

第一种方法(Router.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 路由器(版本 3)进行相对导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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