Check Below Link - it work for me.
As it’s mentioned in the breaking changes, you should use Observables
.
For example, you can create the following service:
import {Injectable} from '@angular/core';
import {Subject} from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class GlobalFooService {
private fooSubject = new Subject<any>();
publishSomeData(data: any) {
this.fooSubject.next(data);
}
getObservable(): Subject<any> {
return this.fooSubject;
}
}
Now, you can subscribe in any component like app.component.ts
:
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.scss']
})
export class AppComponent {
constructor(private globalFooService: GlobalFooService) {
this.initializeApp();
}
initializeApp() {
// other code
this.globalFooService.getObservable().subscribe((data) => {
console.log('Data received', data);
});
}
}
Now, you just have to emit the event from some other component:
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss']
})
export class HomePage {
constructor(private globalFooService: GlobalFooService) {
}
onSomeButtonClick() {
this.globalFooService.publishSomeData({
foo: 'bar'
});
}
}