angular请求示例

方法定义

import {
	HttpClient,
	HttpEventType,
	HttpHeaders,
	HttpParams,
	HttpRequest
} from '@angular/common/http';
import { Observable } from 'rxjs/Observable';

constructor(
	private http: HttpClient
) {}
// 1.GET
/*
	HttpClient.get<any>(url: string, options: {
	    headers?: HttpHeaders | {
	        [header: string]: string | string[];
	    };
	    observe: "response";
	    params?: HttpParams | {
	        [param: string]: string | string[];
	    };
	    reportProgress?: boolean;
	    responseType?: "json";
	    withCredentials?: boolean;
	}): Observable<...>
*/
query(url: string, requestParams: any): Observable<HttpResponse<any>> {
	return this.http.get<any>(`${url}`, {
		observe: 'response',
		params: requestParams
	});
}

// 2.POST
/*
	HttpClient.post<any>(url: string, body: any, options: {
	    headers?: HttpHeaders | {
	        [header: string]: string | string[];
	    };
	    observe: "response";
	    params?: HttpParams | {
	        [param: string]: string | string[];
	    };
	    reportProgress?: boolean;
	    responseType?: "json";
	    withCredentials?: boolean;
	}): Observable<...>
*/
create(url: string, requestBody: any: requestParams: any): Observable<HttpResponse<any>> {
	return this.http.post<any>(`${url}`, requestBody, {
		observe: 'response',
		params: requestParams
	});
}

// 3.PUT
/*
	HttpClient.put<any>(url: string, body: any, options: {
	    headers?: HttpHeaders | {
	        [header: string]: string | string[];
	    };
	    observe: "response";
	    params?: HttpParams | {
	        [param: string]: string | string[];
	    };
	    reportProgress?: boolean;
	    responseType?: "json";
	    withCredentials?: boolean;
	}): Observable<...>
*/
update(url: string, requestBody: any: requestParams: any): Observable<HttpResponse<any>> {
	return this.http.put<any>(`${url}`, requestBody, {
		observe: 'response',
		params: requestParams
	});
}

// 4.DELETE
/*
	HttpClient.delete(url: string, options: {
	    headers?: HttpHeaders | {
	        [header: string]: string | string[];
	    };
	    observe: "response";
	    params?: HttpParams | {
	        [param: string]: string | string[];
	    };
	    reportProgress?: boolean;
	    responseType?: "json";
	    withCredentials?: boolean;
	}): Observable<...>
*/
del(url: string, requestParams: any) {
	return this.http.get<any>(`${url}`, {
		observe: 'response',
		params: requestParams
	});
}


请求示例

// post、put、delete方法类似
this.query(url, params).subscribe(res => {
	console.log(res);
});

使用HttpClient.request()方法发送上传文件请求

多用于上传文件

const formData = new FormData();
// file为需要上传的文件,上传组件中可以拿到
formData.append('files[]', file);
/*
	new HttpRequest<FormData>(method: "POST" | "PUT" | "PATCH", url: string, body: FormData, init?: {
	    headers?: HttpHeaders;
	    reportProgress?: boolean;
	    params?: HttpParams;
	    responseType?: "arraybuffer" | ... 2 more ... | "text";
	    withCredentials?: boolean;
	}): HttpRequest<...>
*/
const req = new HttpRequest(
	'POST',
	url,
	formData,
	{
       	headers: new HttpHeaders({'Authorization': 'Bearer xxx' }),
    	reportProgress: true,
   		params: new HttpParams()
	     .append('param1', p1)
	     .append('param2', p2)
	}
);
this.http.request(req).subscribe(event => {
	// reportProgress: true的响应
    if (event.type === HttpEventType.UploadProgress) {
        const percent = event.loaded / event.total * 100;
        const uploadPercent = percent.toFixed(0);
    }
    if (event.type === HttpEventType.Response) {
    	// 请求完成后的响应
    }
},
err => {
	console.log(err);
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值