Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Angular canDeactivate fails if the back button is hit twice in a row #33956

Closed
aneeskodappana opened this issue Nov 21, 2019 · 5 comments
Closed

Comments

@aneeskodappana
Copy link

aneeskodappana commented Nov 21, 2019

I am attempting to implement a back button guard in Angular 8. I am trying to use a service and canDeactivate.

馃悶 bug report

Affected Package

"@angular/core": "~8.2.2",
"@angular/router": "~8.2.2",

Is this a regression?

yes

Description

Angular 8 canDeactivate fails if the back button is hit twice in a row

馃敩 Minimal Reproduction

// can-deactivate.guard.ts
import { Injectable }    from '@angular/core';
import { CanDeactivate } from '@angular/router';
import { Observable, of }    from 'rxjs';

export interface CanComponentDeactivate {
 canDeactivate: () => Observable<boolean> | Promise<boolean> | boolean;
}

@Injectable({
  providedIn: 'root',
})
export class CanDeactivateGuard implements CanDeactivate<CanComponentDeactivate> {
  canDeactivate(component: CanComponentDeactivate) {
    const confirmation = window.confirm('Is it OK?');

    return of(confirmation);
  }
}
const routes: Routes = [
    { path: 'payment', component: PaymentComponent, pathMatch: 'full', canDeactivate: [CanDeactivateGuard] },
]

馃敟 Exception or Error

Not an error

馃實 Your Environment

Angular Version:


Angular CLI: 8.2.2
Node: 12.4.0
OS: linux x64
Angular: 8.2.2
... animations, cli, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... platform-server, router
@angular-devkit/architect                  0.802.2
@angular-devkit/build-angular              0.802.2
@angular-devkit/build-ng-packagr           0.802.2
@angular-devkit/build-optimizer            0.802.2
@angular-devkit/build-webpack              0.802.2
@angular-devkit/core                       8.2.2
@angular-devkit/schematics                 8.2.2
@angular/cdk                               8.1.3
@angular/common                            9.0.0-next.2
@angular/fire                              5.2.1
@angular/flex-layout                       8.0.0-beta.26
@angular/material                          8.1.3
@angular/material-moment-adapter           8.1.3
@ngtools/webpack                           8.2.2
@nguniversal/express-engine                7.1.1
@nguniversal/module-map-ngfactory-loader   7.1.1
@schematics/angular                        8.2.2
@schematics/update                         0.802.2
ng-packagr                                 5.5.0
rxjs                                       6.5.2
typescript                                 3.5.3
webpack                                    4.38.0

Angular 4 canDeactivate fails if the back button is hit twice in a row

@aneeskodappana aneeskodappana changed the title Angular 4 canDeactivate fails if the back button is hit twice in a row Angular canDeactivate fails if the back button is hit twice in a row Nov 21, 2019
@ajitsinghkaler
Copy link
Contributor

Can I try this one

@aneeskodappana
Copy link
Author

@ajitsinghkaler

resolved manually using this way. any other recommended way link

canDeactivate(component: CanComponentDeactivate, currentRoute: ActivatedRouteSnapshot) {
    const confirmation = window.confirm('Is it OK?');
    if(!confirmation) {
      const currentUrlTree = this.router.createUrlTree([], currentRoute);
      const currentUrl = currentUrlTree.toString();
      this.location.go(currentUrl);
    }

    return of(confirmation);
  }

@ngbot ngbot bot added this to the needsTriage milestone Nov 22, 2019
@ajitsinghkaler
Copy link
Contributor

@pkozlowski-opensource should I implement this

@atscott
Copy link
Contributor

atscott commented Dec 3, 2019

Looks like a duplicate of #13586.

@atscott atscott closed this as completed Dec 3, 2019
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jan 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants