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

119
Visualizações
Angular Catch the back button event for all browsers

I am creating project using angular. In my project i need to handle the browser back button. The requirement is when user click on back button it should be logged out

Login Url is different and dashboard is different.

Below is the implementation

// app.component.ts

@HostListener('window:popstate', ['$event'])
  onBrowserBackBtnClose(event: Event) {
    history.pushState('back', null, null);      
    // log out url
}

But above code is not working when user just landed on the dashboard

Note: It only when user is doing some activity, if user is not doing any activity the event is not firing [chrome]

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I'm not a fan of using window:popstate because it feels like it shouldn't be used in Angular. A better method of listening for popstate events is to subscribe to the location service:

import {Location} from "@angular/common";

constructor(private location: Location) { } 

ngOnInit() { 
    this.location.subscribe(event => {
           //do something
 });
}

a big plus: you can unsubscribe any time you want.

Please keep in mind that as with window:popstate it won't directly detect if you went back or forth. It only fires when it has detected that the route params have changed which ALSO happens when the user hits the forth button.

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