Click here to Skip to main content
15,917,862 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Implemented canDeactive guard, once browser button is clicked it popups a Model and ask Yes/No. After clicking on No it stays on the same page and Again if i press the back button the popup appears and after clicking No/Yes button it redirects to the blank page url from the browser history instead of staying on the same.

Appreciate the support .

What I have tried:

<pre>import { Injectable, HostListener, } from '@angular/core';
import { CanDeactivate, ActivatedRoute, Router, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router';
:
:
:
:

export abstract class myCompCanDeactivate {

  abstract  canDeactivate(): boolean;

    @HostListener('window:beforeunload', ['$event'])
    unloadNotification($event: any) {
        if (!this.canDeactivate()) {
            $event.returnValue = true;
        }
    }
}

@Injectable()

export class DeactivateGuardService implements CanDeactivate<myCompCanDeactivate> {
  showDashboard:boolean=false;
  islogout:any;
  sStep:number;
  demoLastSection:number = 0;
  menuitem:SitefinityPortalMenu;

  constructor(private next:ActivatedRoute,private resourceService: ResourceService,
    private matDialog: MatDialog,private browserGlobalService: BrowserGlobalService, private route:Router ){}
  async canDeactivate(component: myCompCanDeactivate,currentRoute: ActivatedRouteSnapshot, currentState: RouterStateSnapshot, nextState?: RouterStateSnapshot): Promise<boolean> {

    if(!component.canDeactivate()){

      if (this.browserGlobalService.GlobalLogout=== 'logout') {
          this.browserGlobalService.GlobalLogout = '';
          return true;
        }

      //Get Portal left menus from sitefinity.
      this.menuitem = await this.resourceService.getPortalMenus("Portal_Left_Menu").toPromise();


      var str:SitefinityMenuItem[] = this.menuitem.MenuItems.filter((m)=>{return m.LinkUrl.includes(nextState.url) && m.Hidden===false});

      if ( nextState.url!='/demoSite' && nextState.url === (str.length>0?str[0].LinkUrl:'') ){return true; }

      this.sStep = SaleSteps.Review;
      this.demoLastSection = PSSections.signStep;


      if (this.browserGlobalService.PLSavedSection !=  this.demoLastSection || this.browserGlobalService.PLSavedSection != this.sStep ){
      let modalData = new ModalData();
      modalData.Header = this.resourceService.getViewLabel("ModalHeader_BrowserBack"); 
      modalData.Body = this.resourceService.getViewLabel("ModalBody_BrowserMessage"); 
      modalData.ControlArray = [
        new ButtonAction(this.resourceService.getViewLabel("ModalCancel_Yes"), "YES"),
        new ButtonAction(this.resourceService.getViewLabel("ModalCancel_No"), "NO")
      ];
          let dialogRef = this.matDialog.open(GspModalComponent, {
              data:  modalData
          });
          dialogRef.afterClosed().subscribe(result => {
               if (result && result.toLowerCase() === "yes") {
                  this.browserGlobalService.callComponentMethod("");
                  // this.route.navigated = true;
                  return false;
                }
                return false;
          });
        }
    }
    return false;
  }
}
Posted
Comments
Richard Deeming 30-Jan-20 9:40am    
It's not clear to me how the unloadNotification method and the DeactivateGuardService are connected. But you definitely won't be able to call any async methods in the onbeforeunload[^] event.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900