1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

Do I need to use takeUntilDestroyed pipe if I open a subscribtion in the Angular app (root)...

Discussão em 'Angular' iniciado por André, Setembro 10, 2024.

  1. André

    André Guest

    In Angular is good practice to call unsubscribe in open subscriptions on component destroy:

    import { takeUntilDestroyed } from '@angular/core/rxjs-interop';

    export class Component implements OnInit{
    public data;

    constructor(private service: DataService) {
    this.service.getData()
    .pipe(takeUntilDestroyed())
    .subscribe(response => this.data = response);
    }
    }


    However, I was wandering if this is necessary when I'm making a subscription in the Angular app (root) component itself?
    Is it safe to assume that if this component is destroyed, the whole app will be destroyed and the subscription will be closed by default?

    What did I try and what am I expecting?
    I'm not calling takeUntilDestroyed in the app component (which is the root component of my Angular application). I'm expecting that the subscription won't remain open after the component is destroyed.

    Continue reading...

Compartilhe esta Página