本文介绍了Angular 4/5 HttpClient:字符串类型的参数无法分配给"body"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Angular文档说:
The Angular docs say:
http
.get<MyJsonData>('/data.json', {observe: 'response'})
.subscribe(resp => {
// Here, resp is of type HttpResponse<MyJsonData>.
// You can inspect its headers:
console.log(resp.headers.get('X-Custom-Header'));
// And access the body directly, which is typed as MyJsonData as requested.
console.log(resp.body.someField);
});
但是当我尝试这样做时,我得到了一个编译时错误(尽管没有运行时错误,按预期方式工作):
But when I try that, I get a compilation time error (no runtime errors though, works as expected):
为什么?我用"@angular/http": "^5.1.0"
这是我的代码版本:
login(credentials: Credentials): Observable<any> {
const options = {
headers: new HttpHeaders({'Content-Type': 'application/json'}),
observe: 'response'
};
return this.httpClient.post<any>(`${environment.USER_SERVICE_BASE_URL}`,
{'username': credentials.username, 'password': credentials.password}, options)
.map((res) => ...
推荐答案
您必须内联选项.参见 github票证#18586 ,由alxhub
于8月9日输入 2017.
You have to inline the options. See github ticket #18586, entry by alxhub
on August 9 2017.
login(credentials: Credentials): Observable<any> {
return this.httpClient.post<any>(`${environment.USER_SERVICE_BASE_URL}`,
{'username': credentials.username, 'password': credentials.password}, {
headers: new HttpHeaders({'Content-Type': 'application/json'}),
observe: 'response'
})
.map((res) => ...
这篇关于Angular 4/5 HttpClient:字符串类型的参数无法分配给"body"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!