有以下方法可以请求并等待响应:
public async uploadProfile(file: UploadedFile): Promise<ProfileFileResponse> {
return await this.http
.post(his.settings.Config.host + "/api/profiles/upload", file, {
responseType: "json"
}).toPromise();
}
为什么会出现此语法错误:
“对象”类型可分配给很少的其他类型。你的意思是改用“ any”类型?类型“对象”缺少类型'ProfileFileResponse'中的以下属性:状态,文件,profilets(2696)
我可以使用以下方法修复它:
.toPromise() as ProfileFileResponse;
但是对特定类型的响应使用promise的正确方法是什么?
您可以将响应包装在Promise对象中
public async uploadProfile(file: UploadedFile): Promise<ProfileFileResponse> {
return await new Promise((resolve, reject) => {
this.http.post(his.settings.Config.host + "/api/profiles/upload", file, {
responseType: "json"
})
.toPromise()
.then(res => resolve(res.json()), error => reject())
});
}