您可以使用我在
以下示例中的应用程序中使用的 canDeactivate
@Injectable()
export class RouteLinkGuard implements CanDeactivate<Componentchanged> {
constructor(private router: Router, private confirmationService: ConfirmationService, private commonService: CommonService) {}
canDeactivate(Componentchanged: Componentchanged) {
// Here you can use whatever logic you want
if (this.commonService.getChangedValue() === true) {
return Observable.create((observer: Observer<boolean>) => {
this.confirmationService.confirm({
message: 'Are you sure you want to leave this page? Any unsaved data would be lost?',
header: 'Not saved',
icon: 'fa fa-info',
accept: () => {
observer.next(true);
observer.complete();
},
reject: () => {
observer.next(false);
observer.complete();
}
});
});
} else {
return true;
}
}
}
//In Module
// You is like below
const routes: Routes = [
{
path: '',
component: Componentchanged,
canDeactivate: [RouteLinkGuard]
}
];