withNavigationErrorHandler
function
Provides a function which is called when a navigation error occurs.
API
function withNavigationErrorHandler( handler: (error: NavigationError) => unknown): NavigationErrorHandlerFeature;
Description
Provides a function which is called when a navigation error occurs.
This function is run inside application's injection context
so you can use the inject
function.
This function can return a RedirectCommand
to convert the error to a redirect, similar to returning
a UrlTree
or RedirectCommand
from a guard. This will also prevent the Router
from emitting
NavigationError
; it will instead emit NavigationCancel
with code NavigationCancellationCode.Redirect.
Return values other than RedirectCommand
are ignored and do not change any behavior with respect to
how the Router
handles the error.
Usage Notes
Basic example of how you can use the error handler option:
const appRoutes: Routes = [];bootstrapApplication(AppComponent, { providers: [ provideRouter(appRoutes, withNavigationErrorHandler((e: NavigationError) =>inject(MyErrorTracker).trackError(e))) ] });
Jump to details