如何解决在Angular8上从HTTP迁移到HTTPClient
我正在迁移Angular以使用新的HttpClient,但在实现与Http相同的结果时遇到了一些问题。
我要在评论中加入 :
import { Injectable,OnDestroy } from '@angular/core';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Http,Header } from '@angular/http';
//import { HttpClient,HttpHeaders,HttpParams } from '@angular/common/http';
import { Environment } from '../app.component';
//all other component imports
@Injectable()
export class CompanyService implements OnDestroy {
private url = Environment.origin;
private headers = new Headers({
//private headers = new HttpHeaders({
'Accept': 'application/cp+json','Content-Type': 'application/json'
});
constructor(private http: Http) { }
//constructor(private http: HttpClient) { }
buscarEmpresas(pesquisa: string): Observable<any> {
return this.http.get(`${this.url}/companies/${pesquisa}/search`,{ headers: this.headers })
.pipe(map((response) => response.json().response));
}
/* return this.http.get<any>(`${ this.url }/companies/${ pesquisa }/search`,{ headers: this.headers,observe: 'response' } )
* .pipe(map(res => res ));
* -- also tried to stop on headers -- return this.httpClient.get<any>(`${ this.url }/companies/${ pesquisa }/search`,{ headers: this.headers }*/
getEmpresaByUrlLink(urlLink: string): Observable<Retorno<Empresa>> {
return this.http.get(`${this.url}/companies/link/${urlLink}`,{ headers: this.headers })
.pipe(map((response) => new Retorno<Empresa>(response.json())));
}
/* return this.http.get<Retorno<Empresa>>(`${this.url}/companies/link/${urlLink}`,{ headers: this.headers })
* .pipe(map((response) => new Retorno<Empresa>(response)));*/
进行这些更改时,第一种方法什么也不返回,第二种方法返回500。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。