My goal is:
- Allow all the messages of the app to be managed in a single component.
So far I came up with is:
- MessageService: Singleton. Whoever wants to send a message calls addMsg here.
Messages are Subject observables.
newMessages: Subject<IMessage> = new Subject<IMessage>();
- MessageViewer: A component that listens to MessageService and display the messages.
constructor(private _msgService: MessageService) {
this._msgService.getMessages().subscribe(
data => this.showMsg(data)
)
}
Full demo here. https://plnkr.co/edit/aCXMtmiQhRL63EfT5lIy
Is that a good implementation or there is a better way to achieve this?