Skip to content

Commit

Permalink
cleanre logic on canDeactive() (#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
yzlucas authored Feb 1, 2025
1 parent 1e0b643 commit f0b1465
Showing 1 changed file with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ export interface CanComponentDeactivate {
providedIn: 'root'
})
export class CanDeactivateGuard implements CanDeactivate<CanComponentDeactivate> {
canDeactivate(component: CanComponentDeactivate) {
return component?.canDeactivate ? component?.canDeactivate() : true;
canDeactivate(component: CanComponentDeactivate): Observable<boolean> | Promise<boolean> | boolean {
// If the component doesn't have canDeactivate, allow navigation
if (!component?.canDeactivate) {
return true;
}

// Call canDeactivate() and return its result, ensuring undefined/null are treated as true
return component.canDeactivate() ?? true;
}

}

0 comments on commit f0b1465

Please sign in to comment.