我已经实现了 canDeactive 保护,一旦点击浏览器按钮,它就会弹出一个模型并询问是/否。单击否后,它会停留在同一页面上,如果我再次按下后退按钮,则会出现弹出窗口,单击否/是按钮后,它会重定向到浏览器历史记录中的空白页面 url,而不是停留在相同的 url 上。
请找到停用守卫的代码。
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;
}
}