将 NavigationExtras 传递给模板中的 routerLink [英] Pass NavigationExtras to routerLink in template
问题描述
我想在我的 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
我不想在我的控制器中使用 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.
推荐答案
没有办法(目前和 afaik)同时拥有 routerLinkActive
和 skipLocationChange
同时并期待您想要的结果.你可以根据api,添加选项,但是没有skipLocationChange
选项.
There is no way (for now and afaik) to both have routerLinkActive
and skipLocationChange
simultaneously and expect the result you want. You can, according to the api, add options, but there is no skipLocationChange
option.
但是,您可以将路由器的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!