You might recall that in Chapter 2, Personal Trainer, we created the WorkoutResolver
to not only retrieve a workout prior to navigation to the WorkoutComponent
, but also prevent navigation to that component if a non-existent workout was in the route parameters. Now we would like to augment this functionality by displaying an error message on the workouts screen, indicating that the workout was not found.
In order to do this, we are going to modify WorkoutResolver
so that it reroutes to the workouts screen if a workout is not found. To start, add the following child route to WorkoutBuilderRoutingModule
(making sure it precedes the existing workouts route):
children: [
{path: '', pathMatch: 'full', redirectTo: 'workouts'},
{path: 'workouts/workout-not-found', component: WorkoutsComponent'},
{path: 'workouts', component: 'WorkoutsComponent'},
*** other child routes ***
},
]
Next, modify the resolve
method in the WorkoutResolver
to redirect to this route...