排名
14
文章
75
粉丝
21
评论
43
申请别的接口数据(网络接口)
剑轩 :
哇,小姐姐
申请别的接口数据(网络接口)
是伍尚金哇 :
敲一夜代码,流下两三行泪水,掏空四肢五体,六杯白开水七桶泡面
mui框架-移动端跳转以及传值的简单方法(修改解决方法)
剑轩 : 厉害厉害!
mui框架-移动端跳转以及传值的简单方法(修改解决方法)
是伍尚金哇 :
测试了 可以直接在
extras: {
userid:'10'
//自定义扩展...
数据库的varchar和nvarchar的区别
是伍尚金哇 : 没人看 自己看一个 温习一下
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:
50010702506256


欢迎加群交流技术

基于angular的HttpClient封装_HttpClient,直接复制源码即可用
包含常用的get、post、patch、delete和put请求;
import { HttpClient, HttpEvent, HttpHeaders, HttpResponse } from "@angular/common/http";
import { Injectable } from "@angular/core";
import { Observable, of, throwError } from 'rxjs';
import { catchError, switchMap, tap } from 'rxjs/operators';
export type _HttpHeaders = HttpHeaders | { [header: string]: string | string[] };
/**
* 封装HttpClient,主要解决:
* + 优化HttpClient在参数上便利性
* + 统一实现 loading
*/
@Injectable({ providedIn: 'root' })
export class _HttpClient {
constructor(private http: HttpClient) {
}
private _loading = false;
/** 是否正在加载中 */
get loading(): boolean {
return this._loading;
}
private begin(): void {
Promise.resolve(null).then(() => (this._loading = true));
}
private end(): void {
Promise.resolve(null).then(() => (this._loading = false));
}
/**
* GET:返回一个 `any` 类型
*/
get(
url: string,
params?: any,
options?: {
headers?: _HttpHeaders;
observe?: 'body';
reportProgress?: boolean;
responseType: 'arraybuffer';
withCredentials?: boolean;
},
): Observable<any> {
this.begin();
return of(null).pipe(
tap(() => this.begin()),
switchMap(() => this.http.get(url, options)),
tap(() => this.end()),
catchError(res => {
this.end();
return throwError(res);
}),
);
}
/**
* POST:返回一个 `any` 类型
*/
post(
url: string,
body?: any,
params?: any,
options?: {
headers?: _HttpHeaders;
observe?: 'body';
reportProgress?: boolean;
responseType?: 'json';
withCredentials?: boolean;
},
): Observable<any> {
this.begin();
return of(null).pipe(
tap(() => this.begin()),
switchMap(() => this.http.get(url, options)),
tap(() => this.end()),
catchError(res => {
this.end();
return throwError(res);
}),
);
}
/**
* DELETE:返回一个 `any` 类型
*/
delete(
url: string,
params?: any,
options?: {
headers?: _HttpHeaders;
observe?: 'body';
reportProgress?: boolean;
responseType?: 'json';
withCredentials?: boolean;
},
): Observable<any> {
this.begin();
return of(null).pipe(
tap(() => this.begin()),
switchMap(() => this.http.get(url, options)),
tap(() => this.end()),
catchError(res => {
this.end();
return throwError(res);
}),
);
}
/**
* PATCH:返回一个 `any` 类型
*/
patch(
url: string,
body?: any,
params?: any,
options?: {
headers?: _HttpHeaders;
observe?: 'body';
reportProgress?: boolean;
responseType?: 'json';
withCredentials?: boolean;
},
): Observable<any> {
this.begin();
return of(null).pipe(
tap(() => this.begin()),
switchMap(() => this.http.get(url, options)),
tap(() => this.end()),
catchError(res => {
this.end();
return throwError(res);
}),
);
}
/**
* PUT:返回一个 `any` 类型
*/
put(
url: string,
body?: any,
params?: any,
options?: {
headers?: _HttpHeaders;
observe?: 'body';
reportProgress?: boolean;
responseType?: 'json';
withCredentials?: boolean;
},
): Observable<any> {
this.begin();
return of(null).pipe(
tap(() => this.begin()),
switchMap(() => this.http.get(url, options)),
tap(() => this.end()),
catchError(res => {
this.end();
return throwError(res);
}),
);
}
}
评价