I have this interface that i'm using to prevent the user to leave page
export interface ComponentCanDeactivate {
canDeactivate: () => boolean;
}
@Injectable()
export class PendingChangesGuard implements CanDeactivate<ComponentCanDeactivate> {
canDeactivate(component: ComponentCanDeactivate): boolean {
return component.canDeactivate() ?
//code : //more code
}
}
In one of my component i have the following code
export class DashboardComponent implements ComponentCanDeactivate{
@HostListener('window:beforeunload')
canDeactivate(): boolean {
return !this.isDirty;
}
My problem is that my component -> (component: ComponentCanDeactivate) from PendingChangesGuard is always null so i get an error saying
Cannot call canDeactivate() of null
I also have this setup in my routing
path: 'dashboard',
canDeactivate: [PendingChangesGuard],
loadChildren: './views/dashboard/dashboard.module#DashboardModule'
Can someone tell me what am i doing wrong?