Get URL that would be generated by router.navigate
This one works with both: hash (e.g https://site/#/user) and non-hash urls
public openInNewTab(router: Router, namedRoute) {
let newRelativeUrl = router.createUrlTree([namedRoute]);
let baseUrl = window.location.href.replace(router.url, '');
window.open(baseUrl + newRelativeUrl, '_blank');
}
Have a look at createUrlTree
:
this._router.createUrlTree(
['browse'], { queryParams: { section: 'users', isActive: true } });
The returned object, a UrlTree
has a toString
function that should give you what you want.
You can see from the source that navigate
actually uses createUrlTree
internally:
navigate(commands: any[], extras: NavigationExtras = {skipLocationChange: false}):
Promise<boolean> {
validateCommands(commands);
return this.navigateByUrl(this.createUrlTree(commands, extras), extras);
}