import { HttpClient , HttpParams } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; /**
 * Api is a generic REST Api handler. Set your API url first.
 */ @ Injectable ( ) export class Api { url : string = 'http://www.mockhttp.cn/mock/mock' ; constructor ( public http ) { } get ( endpoint , params ? , reqOpts ? ) { if ( ! reqOpts ) { reqOpts = { params : new HttpParams ( ) } ; } if ( params ) { reqOpts . params = new HttpParams ( ) ; for ( let k in params ) { reqOpts . params . set ( k , params [ k ] ) ; } } return this . http . get ( this . url + '/' + endpoint , reqOpts ) ; } post ( endpoint , body , reqOpts ? ) { return this . http . post ( this . url + '/' + endpoint , body , reqOpts ) ; } put ( endpoint , body , reqOpts ? ) { return this . http . put ( this . url + '/' + endpoint , body , reqOpts ) ; } delete ( endpoint , reqOpts ? ) { return this . http . delete ( this . url + '/' + endpoint , reqOpts ) ; } patch ( endpoint , body , reqOpts ? ) { return this . http . put ( this . url + '/' + endpoint , body , reqOpts ) ; } }