In an Angular application, I need for user preferences change to save in the database (by back api). This code should be use in all pages (components).
I got this code :
export class OneComponent implements OnInit, OnDestroy {
watcherSubscriptionForUser: Subscription = Subscription.EMPTY;
async ngOnInit() {
this.watcherSubscriptionForUser = this.libUserService.data.subscribe((currentUser: UserItem) => {
// Preferences changed => call api to save data
});
}
ngOnDestroy(): void {
this.watcherSubscriptionForUser.unsubscribe();
}
}
I did not manage to use Subscription
inside a service.
How can I factorize this peace of code to use for all my concerned components?