简体   繁体   English

在 Angular 6 中单击鼠标中键

[英]Middle click mouse in Angular 6

How to open routerlinks in a new tab with middle click mouse in Angular 6?如何在 Angular 6 中使用鼠标中键在新选项卡中打开路由器链接? I want to open every link in a new tab.我想在新标签页中打开每个链接。 for example例如

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

The auxclick event is fired when a any non-left mouse button has been pressed and released on an element.当在元素上按下和释放任何非鼠标左键时,会触发 auxclick 事件。

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

component.ts组件.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>

and then进而

 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]);
    
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM