【发布时间】:2017-10-22 15:11:59
【问题描述】:
基本上,我在所有调用中都尝试使用 HttpClient 而不是 Http,因此我创建了一个接口来访问响应的某些属性,它对我的大多数调用都运行良好;但问题是我提前输入了这段代码:
const URL = 'https://maps.google.com/maps/api/geocode/json';
this.searchField = new FormControl();
this.results$ = this.searchField.valueChanges
.debounceTime(500)
.switchMap(query => this.http.get(`${URL}?address=${query}`))
.map(response => response)
.map(response => response.results);
我尝试在最后两行将名为 APIResponse 的接口分配给 response,如下所示:.map(response:APIResponse => ...),但它显然会引发语法错误。
我应该在哪里包含响应类型?或者我该如何更改我的代码?
提前致谢。
【问题讨论】:
-
this.http.get<ApiResponse>(...),那么就可以去掉.json()的映射。你没看the docs吗? -
您的标签显示您正在使用
HttpClient,但在使用Http时可以使用json()方法。你用的是哪个?也就是this.http的类型是什么?
标签: angular typescript httpclient