Issue
For each component I create a service which is more or less the same.
E.g.
save(values: any): Observable<Register> {
return this.httpClient.put<Register>(environment.apiBaseUrl + 'register', values);
}
Instead of that, I want a general service that I can use for all components. Somehow like this:
save(values: any, path: string): Observable<Register> {
return this.httpClient.put<Register>(environment.apiBaseUrl + path, values);
}
Then I could use this.myService.save(myObject, 'my-path')
. But what about the type Register
? This time I want the type to be Login
.
Is there a way I could tell the function what type it should be?
Solution
Yes, you can make the Register
type generic:
function save<T>(values: any, path: string): Observable<T> {
return this.httpClient.put<T>(environment.apiBaseUrl + path, values);
}
this is then called with
const observable = save<Register>(values, 'register');
Answered By - Taxel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.