In Angular 7 single page app with "useHash: true" Router module configuration I need to generate url to a resource to be opened in a new tab.
For routing in the app window following code works as expected:
this.router.navigate(['foo', 1]);
That means, it generates url like: http://localhost:4200/#/foo/1
Although when using following method: const url = this.router.createUrlTree(['foo', 1]).toString();
url is "/foo/1" - without the "#", so...
window.open(url, '_blank'); result with NOT valid url:
The only solution (hack) i found is quite brutal:
window.open('#' + url, '_blank');
RouterModule.forRoot(routes, {
useHash: true,
onSameUrlNavigation: 'reload',
}
showItem(item: Item) {
// THIS WORKS AS EXPECTED
this.router.navigate(['item', item.id]);
}
showItemInNewTab(item: Item) {
const url = this.router.createUrlTree(['item', item.id]).toString();
// THIS WORKS BUT ITS A HACK :/
window.open('#' + url, '_blank');
}