Created
June 29, 2022 08:14
-
-
Save ezequieltejada/005b2658921d999d57f589dc9057cb2d to your computer and use it in GitHub Desktop.
Handling subscriptions in Angular components
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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