Angular 财产';文本';不存在于类型';{}';
我使用的角度和我有2个文件提供商 用户服务Angular 财产';文本';不存在于类型';{}';,angular,typescript,ionic-framework,Angular,Typescript,Ionic Framework,我使用的角度和我有2个文件提供商 用户服务 import { HttpClient } from '@angular/common/http'; ... sendTo(address:string, info:string, save:number) { interface UserResponse { success: string; error: string; text: string; id: string; }
import { HttpClient } from '@angular/common/http';
...
sendTo(address:string, info:string, save:number) {
interface UserResponse {
success: string;
error: string;
text: string;
id: string;
}
return new Promise(resolve => {
this.http.get<UserResponse>('http://website.com/api/?action=send&key=' + this.apiKey + '&to=' + address + '&save=' + (save == 1 ? 1:0) + '&info=' + encodeURI(info))
.subscribe(data => {
resolve(data);
}, err => {
console.log(err);
});
});
}
问题出在家里
data.text给我一个错误:类型“{}”上不存在属性“text”
data.success和data.id也会出现同样的问题
您没有指定
sendTo
的返回类型。由于它返回承诺,TypeScript将推断返回类型为Promise
。这意味着传递给的参数将是{}
类型
您可以执行返回新承诺
。但是,您甚至不需要使用Promise构造函数,因为Observable有一个.toPromise
方法:
return this.http.get<UserResponse>(...).toPromise().catch(err => console.log(err));
最后,您可以在home.ts
中使用.subscribe
,而不是使用承诺,尝试使用:返回新承诺
…以及此函数的精确返回类型。
return this.http.get<UserResponse>(...).toPromise().catch(err => console.log(err));
sendTo(address:string, info:string, save:number): Promise<UserResponse> {