#Communication between unrelated components usivng service

9 messages · Page 1 of 1 (latest)

dire kindle
#

So I have Person Component that contains Name string.
and I have List Component.
I want to be able to get that name that's stored inside my Person Component into my List Component, I used a service and it's working, but if im on the list component and I referesh the page, that value coming from Person Component Disappears.
Any help to fix this?

uncut glen
#

That's not about service communication but data persistence. You need to save your data on a database to retrieve it on refresh

proud talon
#

if you want a service, use a behaviourSubject (because why not use observables)

#

and use localstorage

#

but as @uncut glen said, you should persist data in a database

dire kindle
#

@uncut glen @proud talon
Ok let's imagine I have a serivce with get request and it stores it localy in the ts script.
Is there no way to store that request and share it between all components without having to do the request each time?

proud talon
#

of course you can do that 😄

#

there are actually several ways to share this data across the app

#
  • you can use a state management system like NGRX
  • you can create an observable in your service and in your http get subscription, next the result in this observable
  • you can shareReplay of the http call