import {Events} from 'ionic-angular';
import {Events} from '../your/path/to/service/events';
events.subscribe('user:created', (user, time) => {
console.log('Welcome', user, 'at', time);
});
this.events.subscribe('user:created', (data: any) => {
console.log('Welcome', data.user, 'at', data.time);
});
this.events.publish('user:created', someUserInstance, Date.now());
this.events.publish('foo:user:logged-out', {
user: someUserInstance,
time: new Date()
});
const subscription = this.events.subscribe('user:foo:created', (data: any) => {
// your logic
});
Once you are done, you can do this-
subscription.unsubscribe();
Hi @sagrawal31. I eventually discovered it wasn't working in any of my instances where I thought it was working. I think it might have been because the component is getting trashed when navigating to another component? Can't be sure, but the subscription must be dying somewhere.
I may have needed to set up subscriptions elsewhere in a service to get it to work like how I would do with normal angular. My app is a bit complex to copy and paste, it would take a bit of time to create a plunkr for it but basically, all I was doing was creating a subscription to the events in Component A which had been initiated and activated. Then navigated to component B, emitting the event. But the event was not picked up by component A.
I have made a workaround at the moment that is doing what I need it to do. If I get to a point where I desperately need the events as there is no other way to achieve what I want and I run into an issue I will definitely spend the time making a plunkr example.
Thank you for getting back to me though!