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

654
Visualizações
How to close the external window after logout in Angular 12?

I created an external window in the Angular application, everything works fine but I want to close the external window after application logout

written the code below, I'm moving from 1 page to another page then the external window closing properly. If I press the logout button then the external window does not close (ngOnDestroy is not called in the case of application logout)

    ngAfterViewInit() {
    this.externalWindow = window.open('', '', `width=${this.wWidth},height=${this.wHeight},left=${this.wLeft},top=${this.wTop}`)

    this.host = new DomPortalOutlet(
      this.externalWindow.document.body,
      this.componentFactoryResolver,
      this.applicationRef,
      this.injector
    )

    this.host.attach(this.portal)
  }

  ngOnDestroy() {
    this.externalWindow.close()
  }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

this.externalWindow.close()

is indeed the functionality you need to use. The problem is that you need to trigger it upon logout click as well. You can have something like

onClick="closeExternalWindow()"

of course, you need to implement closeExternalWindow which does the logout for externalWindow and then call this function both on destroy and logout click.

about 4 years ago · Juan Pablo Isaza Relatório

0

ngOnDestroy fires only when component destroys. In the logout use case, we are redirecting to the login URL which means it refreshes the page (This is outside angular workflow) hence ngOnDestroy not firing.

If I move 1 route to a different route within the application then ngOnDestroy fires and closes the external window.

This article helped me to find the solution ngOnDestroy not firing on page reload

Solution:

ngOnInit() {
  window.onbeforeunload = () => this.ngOnDestroy()
}
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