I'm trying to detect the router deactivation.
Here's the router definition:
export const AppRoutes: Routes = [
{
path: '',
component: HelloComponent,
canDeactivate: [ConfirmDeactivateGuard]
},
{
path: 'error',
component: ErrorComponent,
canDeactivate: [ConfirmDeactivateGuard],
},
{
path: '**',
component: ErrorComponent,
canDeactivate: [ConfirmDeactivateGuard],
pathMatch: 'full',
}
];
export const AppRouting: ModuleWithProviders = RouterModule.forRoot(AppRoutes, {
enableTracing: true,
});
And this is my deactivate guard service:
@Injectable()
export class ConfirmDeactivateGuard implements CanDeactivate<HelloComponent> {
canDeactivate(target: HelloComponent) {
// This code is never called. Why?
return window.confirm('wtf?' || 'Are you sure?');
}
}
When I try to go from to root url('/') to some other url, I expect to see the window confirmation. However, it is not happening. Do you have any ideas or do you catch anything missing?
If you want to see the code online, check the link below:
https://stackblitz.com/edit/angular-vw9b7u?file=src%2Fapp%2FdeactivateGuard.ts