如果我想将httpClient包装在包装器类中以提供额外的功能。我如何直接从HttpClient获取方法返回类型。我不想为返回类型重新创建所有这些类型的重载。默认情况下,我将丢失所有键入信息。
class myhttp{
constructor(private _http:HttpClient){}
get(url,options){
return _http.get(url,options).pipe(this.customOperator())
}
}
答案 0 :(得分:1)
我通过扩展httpclient类
做了类似的事情import { Injectable } from '@angular/core';
import { HttpClient } from "@angular/common/http";
import { Observable } from "rxjs";
import {tap} from "rxjs/operators"
@Injectable({
providedIn:"root"
})
export class HttpService extends HttpClient {
public get(url , options?:any): Observable<any> {
// do something magical ♂️
return super.get(url,options).pipe(tap(console.log))
}
}
注入HttpService
export class AppComponent {
name = 'Angular';
constructor(private http:HttpService) { }
ngOnInit(){
this.http.get('https://jsonplaceholder.typicode.com/posts').subscribe(console.log);
}