问题描述
我想在Angular 2应用中进行路由时禁止更改URL.
I want to disable changing the URL when routing in my Angular 2 app.
我知道skipLocationChange: true
必须作为NavigationExtras
参数传递给路由器.
I know that skipLocationChange: true
has to be passed as a NavigationExtras
parameter to the router.
我的问题是:
我尝试过的事情:
<h1>
{{title}}
</h1>
<ul>
<li><a [routerLink]="['route1', { skipLocationChange: true }]">Route1</a></li>
<li><a [routerLink]="['route2', { skipLocationChange: true }]">Route2</a></li>
</ul>
<router-outlet></router-outlet>
但它不起作用.
点击链接后,路由更改为http://localhost:4200/route2;skipLocationChange=true
After clicking a link, the route changes to http://localhost:4200/route2;skipLocationChange=true
我不想在控制器中使用this.router.navigate(['route1], {skipLocationChange: true)
,因为那样我会丢失routerLinkAtive
突出显示.
I don't want to use this.router.navigate(['route1], {skipLocationChange: true)
in my controller, since then I lose routerLinkAtive
highlighting.
推荐答案
但是,您可以将路由器中的NavigationExtras
与 ActivatedRoute ,并基于该链接是否应处于活动状态
You can however use the NavigationExtras
from the router in combination with ActivatedRoute and base upon that whether or not the link should be active
更新
自2.3.0-beta.0
起,您可以将skipLocationChange
选项添加到routerLink
指令中:
Since 2.3.0-beta.0
you can add the skipLocationChange
option to the routerLink
directive:
<li><a [routerLink]="['route1']" skipLocationChange>Route1</a></li>
这篇关于将NavigationExtras传递到模板中的routerLink的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!