How can I cancel an ngOnDestoy () event in angular 2+?

1

How can I show a confirmation message when I change the component in Angular 2 .

Example: If I have a form and full information and suddenly press a "Back" button (that sends me to another component) to show a message or alert that says " The information entered will be lost this insurance? YES or NO ", if I press Do not cancel the event ngOnDestroy ().

    
asked by Daniel-David 03.09.2018 в 16:05
source

1 answer

0

The life cycle of the angular components should not be touched, what you have to do is directly prevent your component from being destroyed. How? Well, simply do not remove it from your page. If you have a button that sends you to another component, do something like the following:

In the template:

<button (click)="goBack()">Atrás</button>

In your component:

goBack() {
  if (window.confirm("La información ingresada se perderá ¿Estás seguro?")) {
    //código para mostrar a otro componente, podría ser algo como
    this.router.navigate(['/index']);
  }
}
    
answered by 04.09.2018 в 09:31