Seeking to combine two observables using either forkJoin
or combineLatest
(my current options).
One observable originates from the ActivatedRoute's data (Observable<Data>
) and the other is a call to the API (Observable<Project>
).
https://i.sstatic.net/fu45O.png
Originally, these two were nested and functioning correctly. However, I believe that combining the observables may lead to improvement. I acknowledge that the observable from the activated route is ongoing and is invoked each time the user navigates within the app. The second observable concludes upon receiving a response. forkJoin
operates with completed observables while combineLatest
handles ongoing ones, capturing their latest values.
Is there a way to amalgamate these two, or is the nested approach the optimal solution? Appreciate your insights!
EDIT: Made some minor corrections and introduced array deconstruction to the code.
On a side note, I encountered an issue where upon refreshing the app on the current page, the routeData appears as an empty object. However, placing a breakpoint where the routeData is defined resolves the issue. It seems as though a slight delay is required for the current routeData to be populated. Why isn't it automatically triggered once the data is added to the route? Apologies if this is confusing
Further update:
I have two pages with their respective components: Asset Management (AssetManagementComponent) and Projects Management (ProjectManagementComponent). The Project component is a subcomponent of the Asset component. Upon initializing the asset component, I fetch data from the API and assign it to route.data
. Subsequently, when the project component (child) is loaded, I aim to retrieve the route data along with additional project-specific data from the API.
The issue arises when I refresh the Project Management page, resulting in routeData
being an empty object {}
based on the above code. However, if I navigate from Asset Management to Project Management, routeData
contains the accurate data.
In an attempt to debug this problem using DevTools, I noticed that placing breakpoints in each ngOnInit() function results in the correct routeData
, even when directly refreshing the app on ProjectManagement.
UPDATE:
Here is the final and functional version: https://i.sstatic.net/e0PZZ.png The issue was resolved by adding a filter to the route data observable, ensuring that it first triggers with an empty object and then with a valid object.
Many thanks to all for your valuable input, I have gained new insights and best practices! Wishing you a wonderful day!