From what I've read, the preferred way for unrelated Angular components to communicate is to create a service and use an RxJS BehaviorSubject. Here is one of the many online references to this approach that I found:
https://fireship.io/lessons/sharing-data-between-angular-components-four-methods/
For unrelated components, this site, like others, advocates doing something similar to the following in the service:
private messageSource = new BehaviorSubject('default message');
currentMessage = this.messageSource.asObservable();
changeMessage(message: string) {
this.messageSource.next(message)
}
Then, in the other component:
ngOnInit() {
this.data.currentMessage.subscribe(message => this.message = message)
}
This way is pretty straightforward and I was able to share data using this approach. However, in every reference I've read about subscribing they all say it's important to unsubscribe to avoid a memory leak. There seems to be no way to unsubscribe using this approach. Is there something I'm misunderstanding or some additional step(s) required to avoid a memory leak when using this approach? Is there some approach that I should use instead?