I'm learning to write angular applications in a declarative and I'm not sure what the best approach is when calling POST requests.
I have a login form with email and password forms which when a user enters and clicks submit will call the subject.next()
onLogin() {
if (this.loginForm?.valid) {
this.userService.loginUserSubject.next(this.loginForm?.value)
}
}
Inside my user.service class I have following implemented:
loginUserSubject = new BehaviorSubject<IUser | null>(null);
loginInfo$ = this.loginUserSubject.asObservable();
$login = this.loginInfo$.pipe(
switchMap(user => this.http.put<IUser>('/user/login', user).pipe(
tap(response => localStorage.setItem('token', response.email)),
catchError(error => throwError(error))
)
)
)
Bu then the user clicks submit nothing will happen, the form.value will emit to the subject but the HTTP call won't be executed because there is no subscription happening for the login$ Observable. Is there a way I can call the HTTP POST request when the subject gets the value without subscribing to the Observable in my component or using the async pipe?