I know that when you create and connect a subscription in Angular you should always remember to unsubscribe in the OnDestroy to avoid memory leaks.
this.mySubscription = this.dataService.getAllUsers().subscribe();
ngOnDestroy(){
if(this.mySubscription) this.mySubscription.unsubscribe();
}
However, I frequently see code where a service method is subscribed to and not assigned to a Subscription and so not expressly destroyed.
this.dataService.getAllUsers().subscribe(users => //do something with the users);
Is that bad practice, should you always assign every observable connection you make to a Subscription variable and expressly unsubscribe from it?
Also, if I do create a subscription variable and assign it as above, should I never reuse it within the same component?:
this.mySubscription = this.dataService.getAllUsers().subscribe();
// Later in the component
this.mySubscription = this.dataService.getUserDetail(user.Id).subscribe();
What, if any, are the pitfalls of this kind of reuse?