I'm trying to make some sort of communication between my components, so I'm using a service with a BehaviorSubject and Subscription in the components like this:
Service (code related to the problem):
import { BehaviorSubject } from 'rxjs/BehaviorSubject'
private _clientModalActionSource = new BehaviorSubject<string>('')
modalAction$ = this._clientModalActionSource.asObservable()
public updateClientModalAction(action) {
this._clientModalActionSource.next(action)
console.log('sent')
}
Component A :
this._api.updateClientModalAction(id)
Component B :
import { Subscription } from 'rxjs/subscription'
private _subscription: Subscription
ngOnInit() { this._subscription = this._api.modalAction$.subscribe(res => {
this.refreshModal(res)
console.log('received')
})}
This is working perfectly if Component B is child of Component A, but if A is the root component and B is inside its <router-outlet>
(or the opposite) nothing is being received to the subscription, I only get sent
in the console.
what am I doing wrong?