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

366
Visualizações
How do I make sure that I unsubscribed subscribe?

I have a subscription where I get some messages from the store to output toastr.

I unsubscribed by subscribe .unsubscribe().

How can check that I have really unsubscribe?

  subscription!: Subscription;

  ngOnInit(): void { }

  toastrError(): void {
    this.subscription = this.store.select(getMessage).subscribe(m => {
      (m.message && m.title) ? this.toastr.error(m.message, m.title) : null;
    });
  }

  singIn(): void {
    this.toastrError();
  }

  ngOnDestroy(): void {
    this.subscription.unsubscribe();
  }
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

A component instance has a lifecycle that begins when Angular instantiates the component class and renders the component view (along with its child views). The lifecycle continues with some change detection, as Angular checks to see when data-bound properties change, and in response updates both the view and the component instance when needed. The lifecycle ends when Angular actually destroys the component instance and removes its rendered template from the DOM, so the ngOnDestroy is called immediately before Angular destroys the component (or directive).

If you want to make sure the unsubscription is actually occurring, you can either add a log at the ngOnDestroy method or debug it in the browser.

over 4 years ago · Santiago Trujillo Relatório

0

I think you can use the closed flag after unsubscribe() to validate it. As example

if(!this.subscription.closed)
      console.error("Some error unsubscribing ?!");

If you mean validating outside the component then you probably cannot as the ngOnDestroy will be called only before component instance destruction. You might try to emit an event here but probably will be late to check it !

However, as good practice, you may define a subject in your component and use it to cancel all of your subscriptions using takeUntil(). As example

export class LoginComponent implements OnInit, OnDestroy {

  private destroy$ = new Subject<void>();

  constructor(private http: HttpClient) {}

  ngOnInit(): void {
    this.http.get("http://whatever")
      .pipe(takeUntil(this.destroy$))
      .subscribe(value => console.log(value));
  }

  ngOnDestroy(): void {
    this.destroy$.next();
    this.destroy$.complete();
  }
}

Check this question for difference between such methods

Angular RxJS Observable: takeUntil vs. unsubscribe with a Subscription

over 4 years ago · Santiago Trujillo Relatório

0

I'm guessing you could see that you unsubscribed if no more error toasts would show up. But I get your point. You could either log the ngOnDestroy method to be sure it will run and thus, unsubscribe.

Or you could debug it with something like the demo I've come up with here. By showing/hiding the child component, you're able to see the subscription that is logging a 'ping' message, starts and stops accordingly.

over 4 years ago · Santiago Trujillo 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