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