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

6

31

The following typescript code will always open in the current browser tab

navigate($data: menuItem, $event: JQueryEventObject) {
       //...
       let a = $event.currentTarget as HTMLAnchorElement;
       router.navigate(a.href);    
    }

How do I make router.navigate open in a new tab ? (that is when $event.ctrlKey is true)

Lapidary answered 8/1, 2017 at 9:27 Comment(1)
For now I just use: if ($event.ctrlKey) { window.open(url); }Lapidary
I
13

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); 
}
Iced answered 9/1, 2017 at 8:55 Comment(1)
How do I retrieve url from the Router at least? What if somebody changes routes configuration afterwards?Inconsumable
A
54

this is my solution

const url = this.router.serializeUrl(this.router.createUrlTree(['/my/url/route'], { queryParams: { ...anyQueryParamsYouWantOrOmitThis } }));
window.open(url, '_blank');
Applicant answered 23/8, 2019 at 19:2 Comment(2)
Isn't this an Angular answer?Radicand
Admittedly I didn't see the tags on the question and just assumed they were asking about Angular based on the context of the code. Clearly it's talking about the knockout.js framework called Durandal which uses similar syntax for route navigation, hence my confusion. infoq.com/articles/durandal-javascript-frameworkApplicant
M
24

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');
}
Massa answered 8/4, 2020 at 12:19 Comment(2)
Isn't this an Angular answer?Radicand
@Radicand I think it is by virtue of Router being an angular class.Cahill
I
13

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); 
}
Iced answered 9/1, 2017 at 8:55 Comment(1)
How do I retrieve url from the Router at least? What if somebody changes routes configuration afterwards?Inconsumable
P
4

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>
Physiography answered 18/12, 2017 at 16:50 Comment(0)
N
2

this.router.navigate([]).then((result) => {
  window.open(url, '_blank');
});
Newfoundland answered 7/3, 2022 at 13:42 Comment(0)
I
0

Use this method as a replacement for router.navigate(...) for a new window:

navigateNewWindow(router: Router, commands: any[]) {
    let baseUrl = window.location.href.replace(router.url, '');
    const url = new URL(commands.join("/"), baseUrl).href
    window.open(url, '_blank');
}
Introgression answered 22/6, 2022 at 5:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.