I am currently learning angular and developing a small application a long the way. My application contains multiple components, some of which allowing the use to perform some crud operations. However, I am facing a problem that I have not been able to solve for days now.
The app classify the data in different categories which the user can update. But when the user update the data in one component and then navigates to another. The component the user navigates to does not know of the update and thus does not refresh. I know I could use window.location.reload() to reload the entire app, but I think reloading the application on each update defeats the purpose of building a SPA in the first place.
Is there a way to refresh a component after updating data on another?
To refresh, or better to say update another component from a different component, we can use the concept of Observables and Subject (which is a kind of Observable). This concept has an added benefit when data are to be received from APIs for CRUD operations.
Subject allows values to be multicasted to
Observers, which are registered to listen to the Subject. Subject is like anEventEmitter, in simple terms, if you want to trigger an update, you can emit the new values and it can be sent to all components that you want to update. Technically, we do this by feeding the new value in Subject, by just callingnext(newValue), and then it can be sent to allObserverswho then need tosubscribeit.Taking an example where a message - a string is sent/updated by one component and the other component needs to listen to it and receive/update the same.
We use a common service to communicate between these components.
Now below is a component that wants to update some values/wants to send the message.
Now below is a receiver component that wants to listen to the update
If you want to read more on Subjects, you may see following links:
firebaseapp.com/guide/subject
https://jasonwatmore.com/post
rxjs-understanding-subjects