@angular/router#CanDeactivate TypeScript Examples

The following examples show how to use @angular/router#CanDeactivate. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: editing.guard.ts    From budget-angular with GNU General Public License v3.0 6 votes vote down vote up
@Injectable()
export class EditingGuard implements CanDeactivate<CategoriesComponent> {
  constructor(private dialog: MatDialog) {}

  canDeactivate(categoriesComponent: CategoriesComponent): Observable<boolean> {
    if (categoriesComponent.isAnyFormDirty()) {
      const editingDialog = this.dialog.open(EditingDialogComponent);
      return editingDialog.afterClosed();
    }
    return of(true);
  }
}
Example #2
Source File: prevent-unsaved-changes.guard.ts    From dating-client with MIT License 6 votes vote down vote up
@Injectable({ providedIn: 'root' })
export class PreventUnsavedChangesGuard implements CanDeactivate<unknown> {
  canDeactivate(component: MemberEditProfileComponent | MemberEditAccountComponent,
                currentRoute: ActivatedRouteSnapshot, currentState: RouterStateSnapshot,
                nextState?: RouterStateSnapshot) {

    if (component?.formChanged$.value) {
      return confirm('Are you sure you want to continue? \nAny unsaved changes will be lost!');
    }
    return true;
  }

}
Example #3
Source File: lock-leave.guard.ts    From ng-ant-admin with MIT License 6 votes vote down vote up
/*守卫锁屏页面*/
@Injectable({
  providedIn: 'root'
})
export class LockLeaveGuard implements CanDeactivate<unknown> {
  private routeStatus!: LockScreenFlag;

  constructor(private router: Router, private lockScreenStoreService: LockScreenStoreService) {
    this.lockScreenStoreService.getLockScreenStore().subscribe(res => {
      this.routeStatus = res;
    })
  }

  canDeactivate(
    component: unknown,
    currentRoute: ActivatedRouteSnapshot,
    currentState: RouterStateSnapshot,
    nextState?: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    return !this.routeStatus.locked;
  }

}
Example #4
Source File: can-deactivate.guard.ts    From onchat-web with Apache License 2.0 6 votes vote down vote up
@Injectable({
  providedIn: 'root'
})
export class CanDeactivateGuard implements CanDeactivate<boolean> {
  constructor(
    private globalData: GlobalData,
  ) { }

  canDeactivate(
    component: boolean,
    currentRoute: ActivatedRouteSnapshot,
    currentState: RouterStateSnapshot,
    nextState?: RouterStateSnapshot
  ): boolean | UrlTree | Observable<boolean | UrlTree> | Promise<boolean | UrlTree> {
    return this.globalData.canDeactivate;
  }

}
Example #5
Source File: can-deactivate-dashboard.guard.ts    From 6PG-Dashboard with MIT License 6 votes vote down vote up
@Injectable({
  providedIn: 'root'
})
export class CanDeactivateDashboard implements CanDeactivate<ModuleConfig> {
  canDeactivate(
    component: ModuleConfig,
    currentRoute: ActivatedRouteSnapshot,
    currentState: RouterStateSnapshot,
    nextState?: RouterStateSnapshot) {
    return !Boolean(component.saveChanges?._openedSnackBarRef);
  }
  
}