在Angular版本9及以上,Response
类已被弃用,而被HttpResponse
类所取代。同时,map
操作符已被弃用,推荐使用pipe
操作符配合map
操作符来进行转换。
下面是一个示例代码,展示如何解决该问题:
import { Injectable } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
@Injectable()
export class DataService {
constructor(private http: HttpClient) {}
getData(): Observable {
return this.http.get('your-api-url').pipe(
map((response: HttpResponse) => {
return response.body;
})
);
}
}
在上述代码中,我们使用HttpClient
来发送HTTP请求,并使用pipe
操作符配合map
操作符来转换响应。注意,map
操作符现在是从rxjs/operators
模块中导入的。
这样修改后,你将不再会遇到“类型 'Observable