How to open a new tab with router.navigate in TypeScript
This one works with # hash (like https://example.com/#/users) and non-hash urls
openInNewTab(router: Router, namedRoute) {
let newRelativeUrl = router.createUrlTree([namedRoute]);
let baseUrl = window.location.href.replace(router.url, '');
window.open(baseUrl + newRelativeUrl, '_blank');
}
this is my solution
const url = this.router.serializeUrl(this.router.createUrlTree(['/my/url/route'], { queryParams: { ...anyQueryParamsYouWantOrOmitThis } }));
window.open(url, '_blank');
I think it is better to do in HTML link like this
<a style=" text-decoration: none;" [routerLink]="['your-path',param1,param2]" target="_blank" >your label </a>
Yes, as you @Daneille commented, you have to handle by your own way since durandal's router plugin (navigate function) does not provide a way to open a new tab.
So it is better to handle something as you commented.
if ($event.ctrlKey) {
window.open(url);
}