Shriniwas b
Shriniwas b

Reputation: 336

Subscribe ionic single event in different pages for multiple times

Is it possible to publish a single event in one page and subscribe multiple times on different pages. Like this..

handleClick(){
  this.events1.publish('my-message', '👋 Hello from page1!');
}


export class Page1Page {
  constructor(public events1: Events) {
     events1.subscribe('my-message', (data) =>{
       console.log(data); // 👋 Hello from page1!
     });
  }
}


export class Page2Page {
  constructor(public events1: Events) {
     events1.subscribe('my-message', (data) =>{
       console.log(data); // 👋 Hello from page2!
     });
  }
}

Is it a good practice? Or this code gives error?

Upvotes: 0

Views: 875

Answers (1)

BRass
BRass

Reputation: 3838

OK. Here's my take on this. I see 3 pieces of the puzzle:

1) You need something to tell you about the network connectivity. I would start with the Network cordova-plugin-network-information plugin that is wrapped by Ionic Native.

2) You should create an Angular service/provider that works with this network plugin to be aware of network changes. It can then expose this data for pages to use (and trigger events as needed). Also be aware that the app will likely only here of updates when the app is up, so you may want to get the current status on ready (startup) and resume (open from background). See the Ionic Platform for those hooks.

3) Your pages can then leverage that service/provider from #2 to display the current online status (and possibly watch for changes if your UI needs to alert of change and not just display the current status).

Upvotes: 1

Related Questions