// Part of service
public someEvent: EventEmitter<number> = new EventEmitter();
....
// Component
@Component({
selector: 'some-component',
template: `...`
})
export class SomeComponent {
constructor(public service: Service) {
this.service.someEvent.subscribe((x) => {
// Do something
});
}
}
SomeComponent
is displayed in /
route. When I navigate to different route in my application, and come back again, SomeComponent
will subscribe to the event again, causing callback to fire twice. How to subscribe to the event once or unsubscribe on destroy of the component and subscribe again?
// Can't subscribe after.
ngOnDestroy() {
this.service.someEvent.unsubscribe();
}
subscribe() is an EventEmitter method that registers handlers for events emitted by this instance. subscribe() have three optional parameters which can be used to pass values, errors, or completion notification in EventEmitter .
Since You are not using child to parent component iteration for fetching data from API there is no need to unsubscribe.
Angular will subscribe to the add event and call the addTodo() method with the data when the component triggers the next() method.
Simply use it to emit events from your component. Take a look a the following example. @Component({ selector : 'child', template : ` <button (click)="sendNotification()">Notify my parent! </button> ` }) class Child { @Output() notifyParent: EventEmitter<any> = new EventEmitter(); sendNotification() { this.
Method 1 and 2 are probably the easiest to use in most cases. The subscribe method will return a Subscription instance which is also an EventEmitter. The events are the same as the ones for method 2.
On the update event, a method is run which logs the time. You can add the same listener over and over again, and each one will subscribe to the event. The second argument of the on () function is a callback that can accept any number of the extra data that was emitted by the event.
The emit () function raises the specified event. First parameter is name of the event as a string and then arguments. An event can be emitted with zero or more arguments. You can specify any name for a custom event in the emit () function. You can also use addListener () methods to subscribe for an event as shown below.
The eth subscribe method This method of subscribing to events is like a “catch-all” method. If you want you can listen to all event logs emitted from the blockchain with this method. Method 1 and 2 are probably the easiest to use in most cases. The subscribe method will return a Subscription instance which is also an EventEmitter.
A call to subscribe
returns an instance of Disposable
, which has a method dispose
.
Or if you are using RxJS 5, dispose
has been renamed to unsubscribe
(thanks @EricMartinez).
And from the RxJS docs:
...when we're no longer interested in receiving the data as it comes streaming in, we call dispose on our subscription.
Store the result of your call to subscribe
and later dispose of the subscription within ngOnDestroy
.
export class SomeComponent implements OnDestroy {
constructor(public service: Service) {
this.subscription = this.service.someEvent.subscribe((x) => {...});
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
}
export class SomeComponent implements OnDestroy {
constructor(public service: Service) {
this.subscription = this.service.someEvent.subscribe((x) => {...});
}
ngOnDestroy() {
this.subscription.dispose();
}
}
You can do something like this:
import { OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs/Rx';
export class SomeComponent implements OnDestroy {
private _subscription: Subscription;
constructor(public service: Service) {
this._subscription = this.service.someEvent.subscribe((x) => {
// Do something
});
}
}
ngOnDestroy(){
this._subscription.unsubscribe();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With