Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cancel a subscription in Angular2

How does one cancel a subscription in Angular2? RxJS seems to have a dispose method, but I can't figure out how to access it. So I have code that has access to an EventEmitter and subscribes to it, like this:

var mySubscription = someEventEmitter.subscribe(     (val) => {         console.log('Received:', val);     },     (err) => {         console.log('Received error:', err);     },     () => {         console.log('Completed');     } ); 

How can I use mySubscription to cancel the subscription?

like image 434
Michael Oryl Avatar asked Dec 23 '15 19:12

Michael Oryl


People also ask

Do I need to unsubscribe from Observable?

In Angular applications, it's always recommended to unsubscribe the observables to gain benefits like: Avoids Memory Leaks. Aborting HTTP requests to avoid unwanted calls.

Do we need to unsubscribe subject?

You always have to unsubscribe from: Any Observable or Subject you created manually.


1 Answers

Are you looking to unsubscribe?

mySubscription.unsubscribe(); 
like image 186
kendaleiv Avatar answered Oct 09 '22 04:10

kendaleiv