Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

is subscribing to a lot of events in ngonint bad

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<boolean> = 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()
  }
}
Comment

is subscribing to a lot of events in ngonint bad

@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();
  }
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: Passing a generic function in as a callback in Typescript 
Typescript :: react-i18next bold text 
Typescript :: aws lambda cache gets to next response 
Typescript :: positional arguments dart 
Typescript :: testing with limited information 
Typescript :: to move a directory with its contents in terminal in linux 
Typescript :: a device that interconnects two local area networks that both have a medium access control sublayer. 
Typescript :: middleware in endpoint controller routing-controllers 
Typescript :: typescript reset class properties to default 
Typescript :: flutter: Error: google_fonts was unable to load font LobsterTwo-Bold because the following exception occured: 
Typescript :: sarasota bowling alley bomb threats incident 
Typescript :: formControl Server Side rendering 
Typescript :: nunjucks if logical or 
Typescript :: 3 dots for edit bootstrap 
Typescript :: check if element exists in array java 
Typescript :: typescript object of objects 
Typescript :: check if variable exists python 
Typescript :: laws of ux: using psychology to design better products & services pdf 
Typescript :: tss from gene granges 
Cpp :: hello world c++ 
Cpp :: c++ hide console 
Cpp :: make cin cout faster 
Cpp :: c++ is string a number 
Cpp :: c++ example 
Cpp :: c++ print hello world 
Cpp :: c++ get cursor position console 
Cpp :: inreament operator 
Cpp :: c++ for loop 
Cpp :: c++ vector combine two vectors 
Cpp :: regex for phone number c++ 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =