Does anybody know, what I am doing wrong? I try to get a local json - assign it to a class variable and return the response as an observable — but I am not able to make this work.
Hi
both examples do not show the @Injectable decorator attached to the class, so the HttpClient injection won’t work in the constructor.
Not sure if this helps, but that is at least something I see
I wasn’t paying attention - above example function is returning nothing, not even because there is a return statement in the subscription. And if you would put ‘return’ in front of the http.get then it would not return an Observable, but a Subscription - which by definition won’t hold the data on the stream.
In fact, the subscription is best to be done in the calling function, so outside the service - also to handle the relevant UI in case of errors. RxJS can help transforming the data - if needed.
Big compliments though for the usage of types (Observable<Category>)!
And then the calling function does the subscription handling - via async pipe - so you don’t need to explicitly handle unsubscriptions (not perse an issue with http Observables).
And if you want to monitor the stream just for debugging:
Whether you to store the last known value of the Categories in the service (as per code example of @ciccilleju ) could be a matter of discussion. Do you want to avoid many http calls? If so, then still one should wonder if you would like to do so using an internal variable - as you likely need an Observable getter anyway (using BehaviorSubject) - or do the getting in your lifecycle hooks - but then you need to ensure immutability of the variable, to avoid inconsistent state in your app.
In case you insist on having the service holding the last value in an internal variable, then you could but that assignment in the tap operator.
It works like this now - and yes I want as less calls if possible and cache it in the application… But I prob should change the Observable to an Subject, right? For sure – this JSON file will be replaced with a real API call to a graphQL.
Indeed better, and yes I would create a BehaviorSubject and next the value from the http into it
BehaviorSubject is better than Subject as it provides the last value once someone subscribes to its Observable
The getter function should return the BehaviorSubject as Observable via the asObservable() method or even through typecasting which saves a bit on runtime
The pattern you have now will allow consumers tho change the data within the provide without it knowing
I wouldn’t say BehaviorSubject is always better than an ordinary Subject, only when you have a reasonable default. In a situation where you have to special-case the type to deal with “don’t know yet”, I’d prefer ordinary Subject.