On a basic requirement, I'm trying to implement a Route that will redirect to another route and add a parameter to the Component. This is for an upload management.
Actually we have two routes:
- /documents: Show the list of documents
- /documents/upload: Show the list of documents and make a modal box with the upload form appear
The matter is that I also have GET parameter in the URL that I need to keep. But now, I would like to only the route /documents showed in the URL and not /documents/upload which can be confusing since if we close the modal, it's the same page.
So, my expected result is that going to the page /documents/upload?foo=var&toto=1234 would redirect to /documents?foo=var&toto1234 and keep a variable to tell me that I need to open the modal.
I've created my guard to redirect, but it seems that my queryParams aren't preserved, even with the preserveQueryParams at true. Is it an issue or am I missing something?
canActivate(): boolean {
this.documentsService._isUploading = true;
this.router.navigate(['documents'], { preserveQueryParams: true });
return true;
}
And my route definition:
{ path: 'documents', component: DocumentsComponent},
{ path: 'documents/upload', component: DocumentsComponent, canActivate: [UploadRedirectionGuard]}
Final solution for those who were encountering the same issue:
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
this.documentsService._isUploading = true;
this.router.navigate(['documents'], { queryParams: state.root.queryParams });
return true;
}