i have a feed service in angular that calls a REST endpoint which returns a List of EntryDTO. Here is how the service looks like:
@Injectable({providedIn: 'root'}) constructor(private http: HttpClient) {} export class FeedService() { getPublicFeedEntries: Observable<EntryDTO[]> () { return this.http.get('/rest/publicFeedEntries/).map((response: Response) => response.json()).catch((err: any) => Observable.throw(error.josn().error || 'error'); } } So then i could have a feed component that subscribes to this observable:
export class FeedComponent() { private feedEntries: EntryDTO[]; constructor(private feedService: FeedService) { feedEntries = new Array(); } ngOnInit() { this.feedService.getPublicFeedEntries.subscribe(entries => { this.feedEntries = entries; }, err => {console.log(err) }) } } Is this considered as best practice to return the observable in the service and subscribe in the component or are there other best practices?
.pipe(takeUntil( /* Subject */)). I usually have a propertydestroyed$: Subject<void> = new Subject<void>();and callthis.destroyed$.next()in thengOnDestroyhook. This will stop the subscription once the component is destroyed and will stop any subscription leaking which might affect performance.