Angular 6 routerLink on a new tab

2020-02-14 07:55发布

问题:

I'm trying to open a routerlink on a new tab instead of on the current page but regular html attributes like target _blank are not working.

<span routerLink="/custompage/{{city.id}}"  class="badge badge-primary badge-pill">open</span>

回答1:

I don't think routerLink works with new tabs/windows.

You will have better luck with this, on your component.ts,

window.open(url, '_blank');

or this on your component.html

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

However, if you must use routerLink, there are other ways, such as writing your own custom directives. Check it out over here, that person has already written a sample implementation of it.



回答2:

you can add target="_blank" to your link

it works for me:

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


回答3:

I encountered the exact same issue and i think i've found the best of both worlds.

Change your DIV, SPAN into A tags. Add the href link but keep the routerLink.

<a routerLink="/custompage/{{city.id}}"  href="/custompage/{{city.id}}" class="badge badge-primary badge-pill">open</a>

How it should work now :

  • Left click -> go to corresponding route / page without a new reload ;
  • Right click -> Open in new tab / window options and would naturally load a new page when using this navigation


标签: angular