Skip to content

Instantly share code, notes, and snippets.

@hcastillaq
Created April 29, 2020 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hcastillaq/84bf26acd70324f21cf80673e0776744 to your computer and use it in GitHub Desktop.
Save hcastillaq/84bf26acd70324f21cf80673e0776744 to your computer and use it in GitHub Desktop.
Angular Service - BaserService for HttpRequests, declare all methods and implement this class in other services
import { Injectable } from '@angular/core';
import { HttpClient, HttpParams } from '@angular/common/http';
@Injectable({
providedIn: 'root',
})
export class BaseService {
constructor(private http$: HttpClient) {}
get(url, params: HttpParams = new HttpParams()) {
return this.http$.get(`${url}`, { params });
}
post(url, data) {
return this.http$.post(`${url}`, data);
}
update(url, params: HttpParams = new HttpParams()) {
return this.http$.put(`${url}`, { params });
}
delete(url, params: HttpParams = new HttpParams()) {
return this.http$.request('DELETE', `${url}`, {
body: params,
});
}
put(url, data) {
return this.http$.put(url, data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment