How to open a new tab with router.navigate in TypeScript How to open a new tab with router.navigate in TypeScript typescript typescript

How to open a new tab with router.navigate in TypeScript


this is my solution

const url = this.router.serializeUrl(this.router.createUrlTree(['/my/url/route'], { queryParams: { ...anyQueryParamsYouWantOrOmitThis } }));window.open(url, '_blank');


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


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