Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

105
Vistas
Is there a way to run a specific function before moving or refreshing pages in angular?

When a page is routed to another page or when a user presses the Back, Forward, or Refresh buttons, the alert window is displayed and the user selects Move, and the user wants to proceed with the initialization process.

For unload events, this does not occur in page routing. Attempted to use canDeactivate, but failed to execute a specific function after closing the alert window.

I want to know the detailed method and example. (I'm not familiar with the anglular)

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Yes. I can think of two potential solutions to your problem:

-1- If you are using Angular Material Dialog combined with canDeactivate guard logic there is a built-in method just called like that. It is named beforeClosed. It returns an Observable that you can subscribe to which will notify you when the dialog started closing. Inside this method you can execute your function.

-2- Angular router has an API called events. It essentially helps you track the status of the router. It can detect when the router starts the navigation, ends the navigation, cancels the navigation, resolved the navigation, etc... For a complete list of API advanced events. Check this documentation link Cool, with this API you can choose the right event that suits you and put you function inside the condition such as :

checkRouterEventToFireCustomFunctionLogic(){
    this.router.events.subscribe((routerEvent) => { 
    if(routerEvent instanceof NavigationStart) { 
     // execute my custom function OR 
     // just console.log("Hello World")
    } 
  });
}

PS: beforeunload event is not really good especially when it comes to Web Vitals bfcache

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda