Reputation: 1205
I want to move all the business logic inside the auth service and on the component side i just want to call the method Since none of my functions return anything, is that ok or will they hang?
COMPONENT
credentials: Credentials = {
email: '[email protected]',
password: '123'
}
onLogIn(): void {
this.authService.logIn(this.credentials.email, this.credentials.password);
}
SERVICE
public logIn(email: string, password: string): void {
this.http.post<any>('http://localhost:3100/login', { email, password })
.subscribe(user => {
localStorage.setItem('TOKEN', user.token)
this.router.navigateByUrl('/home');
});
}
Upvotes: 1
Views: 114
Reputation: 11037
Your service methods don't need to return anything, it won't hang. The HttpClient in Angular works using RxJs which is a way of performing asynchronous code.
You can choose to subscribe in (or return the Subscription to) the component instead which would give your component more ability to cancel if you want to.
If you wanted to subscribe in your component, you can use tap
to get the resulting data you need.
To me, any of these ways are perfectly valid ways of setting up your code.
Upvotes: 1