Skip to content

Instantly share code, notes, and snippets.

@steph-crown
Created December 10, 2024 21:50
Show Gist options
  • Save steph-crown/bfe6b0fb00950d4a761442f757fdc17a to your computer and use it in GitHub Desktop.
Save steph-crown/bfe6b0fb00950d4a761442f757fdc17a to your computer and use it in GitHub Desktop.
// ...
export class MemoryLeakComponent implements OnInit, OnDestroy {
constructor(private readonly router: Router) {}
count = 0;
private sub: Subscription | undefined;
ngOnInit() {
this.sub = interval(1000).subscribe(() => {
this.count++;
console.log('Count:', this.count); // This will keep logging as more subscriptions are created
});
}
goBack() {
this.router.navigate(['/']);
}
ngOnDestroy() {
if (this.sub) {
this.sub.unsubscribe();
}
console.log('Unsubscribed and cleaned up');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment