Skip to content

Instantly share code, notes, and snippets.

@ezequieltejada
Created June 29, 2022 08:14
Show Gist options
  • Save ezequieltejada/005b2658921d999d57f589dc9057cb2d to your computer and use it in GitHub Desktop.
Save ezequieltejada/005b2658921d999d57f589dc9057cb2d to your computer and use it in GitHub Desktop.
Handling subscriptions in Angular components
import { Component, OnInit } from '@angular/core';
import { Service } from '../services/service';
@Component({
selector: 'selector-name',
templateUrl: 'name.component.html'
})
export class NameComponent implements OnInit, OnDestroy {
subscriptions$: SubscriptionLike[] = [];
constructor(private service: Service) { }
ngOnInit() {
this.subscriptions$.push(
this.service.getData().subscribe()
);
}
ngOnDestroy() {
this.subcriptions$.forEach(sub => sub.unsubscribe());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment