utilize a service
import { Injectable } from '@angular/core';
import { Observable, Subject } from 'rxjs';
@Injectable()
export class MessageService {
private _message: Subject<any>;
constructor() {
this._message = new Subject();
}
get updates(): Observable<any> {
return this._message.asObservable();
}
set communication(message: any) {
this._message.next(message);
}
}
module one
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Component({
selector: 'app-one',
templateUrl: './one.component.html',
styleUrls: ['./one.component.scss'],
})
export class OneComponent {
constructor(private _http: HttpClient, private _message: MessageService) { }
handleAPICall(): void {
this._http.get('end-point').subscribe(value => this._message.communication = value);
}
}
module two
import { Component } from '@angular/core';
@Component({
selector: 'app-two',
templateUrl: './two.component.html',
styleUrls: ['./two.component.scss'],
})
export class TwoComponent {
constructor(private _message: MessageService) {
this._message.updates.subscribe(value => console.log(value));
}
}