I am trying to understand subscriptions in angular and after some reading I have taken the below approach.
I am using .pipe(takeUntil()) in order to unsubscribe.
Is this the right way to check if the subscription has successfully unsubscribed
// subject I'm using to unsubscribe subscriptions
private onDestroy$ = new Subject();
// The call to load data
this.service.method()
.pipe(
takeUntil(this.onDestroy$),
tap(() => console.log('unsubscribed')) // <== does this run when it unsubscribes ?
)
.subscribe((pData => {
});
OnDestroy:
ngOnDestroy() {
this.onDestroy$.next(); <== is this necessary ? I'm not sending data
this.onDestroy$.complete();
}
I have aslo read that you do not need to unsubscribe from HTTP requests as it runs the complete() after getting the result and unsubscribes itself, is that correct?
Can you please explain this next statement to me aswell:
"Using an operator like takeUntil instead of manually unsubscribing will also complete the observable, triggering any completion event on the observable."