Angular 2 中的被动链接 - <a href=">相等的 [英] Passive Link in Angular 2 - <a href=""> equivalent
问题描述
在 Angular 1.x 中,我可以执行以下操作来创建一个基本上什么都不做的链接:
In Angular 1.x I can do the following to create a link which does basically nothing:
<a href="">My Link</a>
但在 Angular 2 中,相同的标签会导航到应用程序库.在 Angular 2 中,它的等价物是什么?
But the same tag navigates to the app base in Angular 2. What is the equivalent of that in Angular 2?
它看起来像 Angular 2 路由器中的一个错误,现在有 github 上的一个未解决问题.
It looks like a bug in the Angular 2 Router and now there is an open issue on github about that.
我正在寻找开箱即用的解决方案或确认不会有任何解决方案.
I am looking for an out of the box solution or a confirmation that there won't be any.
推荐答案
如果你有 Angular 5 或更高版本,只需更改
If you have Angular 5 or above, just change
<a href="" (click)="passTheSalt()">Click me</a>
进入
<a [routerLink]="" (click)="passTheSalt()">Click me</a>
将鼠标悬停在链接上时会显示带有手形图标的链接,单击它不会触发任何路线.
A link will be displayed with a hand icon when hovering over it and clicking it won't trigger any route.
注意:如果要保留查询参数,应将queryParamsHandling
选项设置为preserve
:
Note: If you want to keep the query parameters, you should set queryParamsHandling
option to preserve
:
<a [routerLink]=""
queryParamsHandling="preserve"
(click)="passTheSalt()">Click me</a>
这篇关于Angular 2 中的被动链接 - <a href=">相等的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!