I've created this HttpService which leverages the HttpClient. In the service i have 1 method called getUserId() that is making a http request to a mock api. In the response I wan't to grab the userId property and have the getUserId() return the userId.
The idea is to be able to get the userId in a component by creating an instance of the HttpService and call getUserId() method. I don't know if this is the right approach but the purpose of the httpservice is to handle all http requests and by creating an instance of it in a component i can access all it's functions depending on what data i need. I am completely new at Angular.
How do i make it so that getUserId() returns the userId property of the http response?
httpService:
import { Injectable } from '@angular/core';
import { HttpClient} from '@angular/common/http';
interface IuserData{
userId: number;
}
@Injectable({
providedIn: 'root'
})
export class HttpService {
constructor(private http: HttpClient) { }
getUserId(){
return this.http.get<IuserData>("https://jsonplaceholder.typicode.com/todos/1").subscribe(data => {
console.log('User ID: ' + data.userId)
})
}
}