angular 2frxjs when should i unsubscribe from 60subscription 60

Solutions on MaxInterview for angular 2frxjs when should i unsubscribe from 60subscription 60 by the best coders in the world

showing results for - "angular 2frxjs when should i unsubscribe from 60subscription 60"
Esteban
18 Oct 2017
1// You don't need to have bunch of subscriptions and unsubscribe manually.
2// Use Subject and takeUntil combo to handle subscriptions like a boss:
3
4
5import { Subject } from "rxjs"
6import { takeUntil } from "rxjs/operators"
7
8export class ViewRouteComponent implements OnInit, OnDestroy {
9  componentDestroyed$: Subject<boolean> = new Subject()
10
11  constructor(private titleService: TitleService) {}
12
13  ngOnInit() {
14    this.titleService.emitter1$
15      .pipe(takeUntil(this.componentDestroyed$))
16      .subscribe((data: any) => { /* ... do something 1 */ })
17
18    this.titleService.emitter2$
19      .pipe(takeUntil(this.componentDestroyed$))
20      .subscribe((data: any) => { /* ... do something 2 */ })
21
22    //...
23
24    this.titleService.emitterN$
25      .pipe(takeUntil(this.componentDestroyed$))
26      .subscribe((data: any) => { /* ... do something N */ })
27  }
28
29  ngOnDestroy() {
30    this.componentDestroyed$.next(true)
31    this.componentDestroyed$.complete()
32  }
33}
34