What do I wanna do?
I want to implement a button which opens a URL in a new Tab. The URL contains a query parameter consisting of numbers taken out of the "displayData" object.
Description of the problem:
I have a route which is defined like this in the app.routes.ts:
{
path: 'displays/:displayIds',
loadChildren: () => import('module').then(m => m.module),
canActivate: [RoleGuardService],
data: {
expectedRole: 'ROLE_READ'
}
}
Next, I've got a HTML-Button which is calling the following method:
openInNewTab(e: Event): boolean {
e.preventDefault();
const qp = {displayIds: this.displayData.map(display => display.deviceID).join(',')};
const urltree = this.router.createUrlTree(['#/displays'], {queryParams: qp} );
this.router.navigate([]).then(result=> {
window.open(unescape(decodeURIComponent( this.router.serializeUrl(urltree) )), '_blank');
});
}
When you log the resulting URL, you get the following string:
http://localhost:4200/#/displays?displayIds=14361,14510
Which is exactly the one I want. But now is where the problem comes into place: If I click the button to open the URL in a new Tab, the correct URL is displayed in the searchbar for about a second. After that, the site goes blank and the error "Cannot match any routes. URL Segment: 'displays%3FdisplayIds%3D14361,14510'" appears. If I take the exact same URL mentioned above this paragraph and paste it in the same tab which was opened before, the routing is done successfully and the site is displayed correctly.
My question is, why does the site only load if I paste the URL manually in the already opened site and how do I change the behaviour so that the site is displayed correctly on opening it per the button?
Edit:
For anyone that seeks a solution to the problem, the following configuration solved it for me:
I configured the route in app.routes.ts like this:
{
path: 'displays',
loadChildren: () => import('module').then(m => m.module),
canActivate: [RoleGuardService],
data: {
expectedRole: 'ROLE_READ'
}
}
In the module which is referenced in "loadChildren" I added the following configuration:
const routes: Routes = [
{
path: 'displays/:Ids',
component: DisplaysComponent
}
];
Now, if I call the URL like this 'http://localhost:4200/#/displays/3737" it's resolved as wished and redirects me to the correct page. I hope I was able to help y'all!