I've been developing an Angular 7 application and using BehaviorSubject to manage the user authentication state, following the widely recommended practice from various sources online.
However, I've come across a puzzling issue - given that BehaviorSubject is an Observable, why am I unable to trigger the onComplete() method?
Here's the code snippet I'm working with, which appears quite standard:
this.authService.authenticationState.subscribe(state => {
this.isLoggedIn = state;
},
err => console.log(err),
() => console.log('complete')
);
The authService instance setup looks like this:
authenticationState = new BehaviorSubject(false);
Despite my efforts, 'complete' doesn't get printed in the console. Could there be a mistake in my approach?
SOLUTION
this.authService.authenticationState.subscribe(state => {
this.isLoggedIn = state;
this.authService.authenticationState.complete();
},
err => console.log(err),
() => console.log('complete')
);
After making this adjustment, the complete() method successfully gets executed.