Created
May 23, 2024 07:57
-
-
Save rakia/53315b3a3073650f292c822e1d24f147 to your computer and use it in GitHub Desktop.
Angular Routing: redirectTo as function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const routes: Routes = [ | |
{ path: "dashboard", component: DashboardComponent }, | |
{ | |
path: "profiles", | |
redirectTo: ({ queryParams }) => { | |
const errorHandler = inject(ErrorHandler); | |
const userId = queryParams['userId']; | |
if (userId) { | |
// return the appropriate redirect path based on the userId query-parameter | |
return `/profiles/${userId}`; | |
} else { | |
// Handle "userId is missing" error | |
errorHandler.handleError(new Error('Cannot navigation to profile page without userID.')); | |
// Redirect to error page | |
return `/error`; | |
} | |
}, | |
}, | |
{ path: "profiles/:userId", component: OtherComponent }, | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment