将 NavigationExtras 传递给模板中的 routerLink [英] Pass NavigationExtras to routerLink in template

查看:24
本文介绍了将 NavigationExtras 传递给模板中的 routerLink的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 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)同时拥有 routerLinkActiveskipLocationChange同时并期待您想要的结果.你可以根据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.

但是,您可以将路由器的 NavigationExtrasActivatedRoute 并以此为基础确定链接是否应处于活动状态

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屋!

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