Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

116
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda