Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

214
Views
Eliminando el evento beforeunload en angular

Estoy tratando de eliminar el evento usando window.removeEventListener que he agregado usando window.addEventListener para el evento beforeunload pero no se elimina. Estoy tratando de seguir el código

 addAlertWithListener(){ window.addEventListener('beforeunload', (event) => { // Cancel the event as stated by the standard. event.preventDefault(); // Chrome requires returnValue to be set. event.returnValue = 'You are currently in edit mode. Are you sure you want to refresh the page?'; },true); }

Se está agregando, pero cuando intento eliminar la misma función usando el siguiente código:

 removeAlertWithListener() { window.removeEventListener('beforeunload', null,true); }

Todavía da mensaje de alerta. ¿Me estoy perdiendo algo aquí? Probé con y sin el último parámetro. Ninguna de las cosas parece funcionar aquí.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

addEventListener y removeEventListener se implementan de una manera extraña en JavaScript, necesitan exactamente los mismos parámetros para recuperar los oyentes registrados. Eso es diferente de setTimeout donde se devuelve una identificación.

En Angular, generalmente hago algo como esto:

 private readonly BEFORE_UNLOAD_PARAMS = ['beforeunload', (event) => ..., true]; private isBeforeUnloadActive: boolean = false; addAlertWithListener() { if (this.isBeforeUnloadActive) return; window.addEventListener(...this.BEFORE_UNLOAD_PARAMS); this.isBeforeUnloadActive = true; } removeAlertWithListener() { window.removeEventListener(...this.BEFORE_UNLOAD_PARAMS); this.isBeforeUnloadActive = false; }

La bandera evitará que el oyente se registre dos veces

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!