Lately, I have been diving into a new Angular project and noticed that the common way to share state between unrelated components is by using rxjs Subject
/BehaviorSubject
as static members within the class.
For instance:
export class AbcService {
private static importantData: Subject<any> = new Subject();
static get importantData$(): Observable<any>{
return AbcService.importantData.asObservable()
}
}
How it's used:
@Component({
...
})
export class DefComponent {
constructor() {
AbcService.importantData$.subscribe(...)
}
}
I've been racking my brain trying to understand the benefits of this approach but struggle to see any real value in it, especially considering it goes against the injection concept.
Am I missing something here? What advantages come with declaring observables as static class members?