在Angular 6中单击鼠标中键



如何在Angular 6中用鼠标中键打开新选项卡中的路由链接?我想打开新选项卡中的每个链接。例如

<button mat-icon-button color="accent" [routerLink]="['/edit', a.Id]"> <mat-icon>edit</mat-icon> </button>

当在元素上按下并释放任何非鼠标左键时,auxclick事件就会触发。

<button mat-icon-button color="accent" [routerLink]="['/edit', a.Id]" 
(auxclick)="onClick($event)">
<mat-icon>edit</mat-icon>
</button>

组件.ts

onClick(e){
e.preventDefault();
if(e.which==2){
window.open('/users/'+a.Id);
}
}
<a  (click)="open(a.Id,$event)" href="/edit/{{item.Id}}" target="_blank">
<mat-icon>edit</mat-icon>
</a>

然后

open(id: number, event: MouseEvent) {
// prevent href to fire.
// href will work only for middle mouse button click
event.preventDefault(); 
// open in new tab when click + ctrl
if (event.ctrlKey) {
return window.open('/edit/' + id, '_blank')
}

this.router.navigate(['/userAd', id]);

}

最新更新