I have a service with a method where I emit an observable, then in another service I want to subscribe to that observable, what's the best way to do this?
aServiceMethod(value){
this.serviceA.obs$.next(value);
}
bServiceMethod(){
let method B = //get value from obs
}
Subscribe the observable in bServiceMethod and use take(1)?
Subscribe the observable in the constructor of service B?
Transform the observable into behaviorSubject and use getValue()?
Other?
I think using a BehaviorSubject
here would be the best strategy to keep track of the value of interest globally (between the different services)
Emitting the value from the aServiceMethod
method, in the tune of:
export class ServiceA {
obs$ = new BehaviorSubject<any>(null);
aServiceMethod(value: any){
this.obs$.next(value);
}
}
and subscribing to the observable in bServiceMethod
:
export class ServiceB {
constructor(private serviceA: ServiceA) {
this.bServiceMethod();
}
bServiceMethod() {
this.serviceA.obs$.subscribe(value => {
let methodB = value;
console.log(methodB);
});
}
}