Ionic 4 Events not working in device but working on browser

To update Data from one page to other we used Events library. but events are no longer available in ionic 5.
blow is the solution.
run command:

ionic generate service events        // this will create events provider

copy paste blow code.

import { Injectable } from '@angular/core';
import {Subject} from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class EventsService {

private fooSubject = new Subject<any>();

constructor() { }


    publishLogin(data: any) {
        this.fooSubject.next(data);
    }

    receiveLogin(): Subject<any> {
        return this.fooSubject;
    }
}

From Page A:
import your service
initialize it in constructor //

constructor(public events: EventsService){}

and publish event E.g.

 this.events.publishLogin(yourDataVariable);

Receive it in Page B:
import your service
initialize it in constructor //

    constructor(public events: EventsService){}

this.events.receiveLogin().subscribe((res:any)=>{
        console.log(res);
      })

Leave a Comment