Skip to content

Instantly share code, notes, and snippets.

@rakia
Created May 23, 2024 07:57
Show Gist options
  • Save rakia/53315b3a3073650f292c822e1d24f147 to your computer and use it in GitHub Desktop.
Save rakia/53315b3a3073650f292c822e1d24f147 to your computer and use it in GitHub Desktop.
Angular Routing: redirectTo as function
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