Angular 6 routerLink on a new tab

Here's an alternate way of doing within a component.

openCityInNewWindow(city) {
  // Converts the route into a string that can be used 
  // with the window.open() function
  const url = this.router.serializeUrl(
    this.router.createUrlTree([`/custompage/${city.id}`])
  );

  window.open(url, '_blank');
}

Html will look something like

<ul *ngFor="let city of cities">
  <li (click)="openCityInNewWindow(city.id)">{{city.name}}</li>
</ul>

There is a newer way of opening new tabs using routerLink, but in my opinion, this is still a simpler option, as both methods still require to make use of window.open().

On your component.ts,

openNewTab(url) {
  window.open(url, '_blank');
}

or this on your component.html

<a href="www.domain.com/custompage/{{city.id}}" target="_blank">

Another valid alternative is to write your own custom directives. Check it out over here, that person has already written a sample implementation of it.


you can add target="_blank" to your link

it works for me:

 <a routerLink="/News" routerLinkActive="active" target="_blank" rel="bookmark"></a>

Tags:

Angular