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)
Yes it can be attached to div tag, your route is probably wrong try add / in front of route.
Using Router linksAfter Angular v4 we can directly add a routerLink attribute on the anchor tag or button.
Angular router traverses the URL tree and matches the URL segments against the paths configured in the router configuration. If a URL segment matches the path of a route, the route's child routes are matched against the remaining URL segments until all URL segments are matched.
this is my solution
const url = this.router.serializeUrl(this.router.createUrlTree(['/my/url/route'], { queryParams: { ...anyQueryParamsYouWantOrOmitThis } }));
window.open(url, '_blank');
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');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With