After researching different methods, it appears that the recommended way for unrelated Angular components to communicate is by creating a service and utilizing an RxJS BehaviorSubject. A helpful resource I came across outlining this approach can be found here:
For communication between unrelated components, many sources suggest implementing something similar to the following structure within the service:
private messageSource = new BehaviorSubject('default message');
currentMessage = this.messageSource.asObservable();
changeMessage(message: string) {
this.messageSource.next(message)
}
And then in the other component:
ngOnInit() {
this.data.currentMessage.subscribe(message => this.message = message)
}
While this method proved effective for data sharing, there's one concern - all references mention the importance of unsubscribing to prevent memory leaks when subscribing. Despite this, there doesn't seem to be a direct way to unsubscribe using this approach. Is there a misunderstanding on my end, or are there additional steps needed to ensure no memory leaks occur with this method? Are there alternative approaches that should be considered instead?