'Angular 1 http service for all components
For each component I create I'm creating a service for it for http requests. I cannot have only one since i'm passing the <udcInterface[]> as below:
getResponce() {
return this.http.get<udcInterface[]>(`${this.baseLink}`,
this.httpOptionsWithToken)
}
is there a way where I can pass it in the function? something like the below (I know that it is wrong)
getResponce(something) {
return this.http.get<something[]>(`${this.baseLink}`,
this.httpOptionsWithToken)
}
for each component, I'm creating the same HTTP request code and only changing <udcInterface[]>
Solution 1:[1]
In the <> brackets you're just declaring the return type of the generic function get. You can make any function generic using these brackets.
getResponce<T>(): Observable<T> {
return this.http.get<T>(`${this.baseLink}`, this.httpOptionsWithToken);
}
Then to call the function it's the same as you did with get
const response = this.getResponce<string[]>();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chris Hamilton |
