7.2: Add CanDeactivate Guard
Right now, if our user is editing their profile and they click away - all of their changes will be lost.
We can add a guard that sends a dialogue box asking them to confirm if they want to navigate away and lose their changes. Instead of a guard to protect a route form being activated as we've done previously - we can add a guard that prevents a route from being deactivated.
Adding Guard
In the guards folder, add a new file called prevent-unsaved-changes.guard.ts
.
We'll also need to import it into our app.module.ts
file and add it to the providers array:
Adding Guard to Route
In routes.ts
, add the new Guard to the MemberEditComponent route:
Last updated