【发布时间】:2019-06-28 23:46:55
【问题描述】:
我遇到了一个以前从未遇到过的奇怪错误,并且找不到相关的帖子。
错误是:
Property 'then' does not exist on type '{ <T>(this: Observable<T>): Promise<T>; <T>(this: Observable<T>, PromiseCtor: PromiseConstructor)...'.
我正在从使用.subscribe 切换到使用.toPromise().then()。我对其他几个 HTTP 请求做了同样的事情,没有问题。不知道这个有什么不同。
在服务中我有这个功能:
getData(user: string) {
var url = some url
let httpOptions = {
headers: new HttpHeaders({
some headers
}),
withCredentials: true };
return this.http.get(url, httpOptions);
}
然后在一个组件中我有
this._apiService.getData(lan).toPromise.then((information: any) =>
{
console.log(information)
});
我在其他三个http 请求上做了一些非常相似的事情而没有遇到这个问题,并且服务调用的设置完全相同,组件功能也是如此。这个错误是什么意思?
【问题讨论】:
-
不要忘记括号:
.toPromise().then(...)。 -
toPromise是一个函数。 -
简单的错字,现在可以关闭问题了。