I think you could use an EventEmitter
defined in the service and not inside the component itself. This way, your component will use this service attribute to retrieve the event.
import {EventEmitter} from 'angular2/core'; export class NotificationService { onDestroyEvent: EventEmitter<string> = new EventEmitter(); constructor() {} } export class MyComponent implements OnDestroy { constructor(service:NotificationService) { this.service = service; } ngOnDestroy() { this.service.onDestroyEvent.emit('component destroyed'); } }
Other elements / components may subscribe to this EventEmitter for notification:
this.service.onDestroyEvent.subscribe(data => { ... });
Hope this helps you, Thierry
source share