is subscribing to a lot of events in ngonint bad is subscribing to a lot of events in ngonint bad

The solution for “is subscribing to a lot of events in ngonint bad is subscribing to a lot of events in ngonint bad” can be found here. The following code will assist you in solving the problem.

import { Subject } from “rxjs”
import { takeUntil } from “rxjs/operators”

@Component({
moduleId: __moduleName,
selector: “my-view”,
templateUrl: “../views/view-route.view.html”
})
export class ViewRouteComponent implements OnInit, OnDestroy {
componentDestroyed$: Subject = new Subject()

constructor(private titleService: TitleService) {}

ngOnInit() {
this.titleService.emitter1$
.pipe(takeUntil(this.componentDestroyed$))
.subscribe((data: any) => { /* … do something 1 */ })

this.titleService.emitter2$
.pipe(takeUntil(this.componentDestroyed$))
.subscribe((data: any) => { /* … do something 2 */ })

//…

this.titleService.emitterN$
.pipe(takeUntil(this.componentDestroyed$))
.subscribe((data: any) => { /* … do something N */ })
}

ngOnDestroy() {
this.componentDestroyed$.next(true)
this.componentDestroyed$.complete()
}
}@Component({ … })
export class SmartComponent implements OnInit, OnDestroy {
private subscriptions = new Subscription();

constructor(private heroService: HeroService) {
}

ngOnInit() {
this.subscriptions.add(this.heroService.getHeroes().subscribe(heroes => this.heroes = heroes));
this.subscriptions.add(/* another subscription */);
this.subscriptions.add(/* and another subscription */);
this.subscriptions.add(/* and so on */);
}

ngOnDestroy() {
this.subscriptions.unsubscribe();
}
}

Thank you for using DeclareCode; We hope you were able to resolve the issue.

More questions on [categories-list]

0
inline scripts encapsulated in