angularjs - ReactiveX JS and TypeScript - How to unsubscribe? -
i've been attempting implement new rxjs observable
in angular 2 component using typescript. have created service, myservice
returns observable in 1 of methods, e.g.,
export class myservice { getmyobs(){ return new observable(observer => { observer.next(42); }); } }
then in angular 2 component subscribe observable in oninit, e.g.,
export class mycomponent implements oninit { obs: any; constructor(private myservice: myservice){}; ngoninit { this.obs = myservice.getmyobs().subscribe(data => { // stuff here... }); } }
the rxjs documentation talks unsubscribing observable such observable knows no longer emit messages observer. therefore, figured should unsubscribing observable when component gets destroyed, like
export class mycomponent implements oninit, ondestroy { obs: any; constructor(private myservice: myservice){}; ngoninit { this.obs = myservice.getmyobs().subscribe(data => { // stuff here... }); } ngondestroy { this.obs.unsubscribe(); } }
whilst makes sense me, typescript compiler throws (and indeed application throws) saying there no unsubscribe
method. there appears no such method described in type definition files. how correctly unsubscribe observable using typescript?
you need add unsubscribe method observable
., disposing observable executions
return new observable(observer => { observer.next(42); return () => { console.log('unsubscribe') } });
Comments
Post a Comment