Angular如何在http文章中定义多个观察



早上好,在Httppost调用中,我需要读取响应中的标头和事件,以便在调用过程中管理进程。为了管理进度,我设置了观察:"事件",它非常有效。我还需要设置观察:"响应"来读取答案的标题。我该怎么做?

return this.http.post(ip + this.apiDownFile,JSON.stringify(paramObj),{
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json'
},
responseType: 'blob' as 'json',
reportProgress: true,
observe: 'events'
}).pipe(
map(risposta=>risposta)
)

您不需要另外观察响应。如果您正在观察"事件",则可以获得响应,因为响应是您应该接收的事件的一部分。

如果您想对响应执行某些操作,请添加rxjstap运算符,然后检查该事件是否为HTTP响应。您可以通过提供角度为-HttpEventTypeenum来实现这一点。

导入:

import { HttpEventType } from "@angular/common/http";

代码:

return this.http.post(ip + this.apiDownFile,JSON.stringify(paramObj),{
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json'
},
responseType: 'blob' as 'json',
reportProgress: true,
observe: 'events'
}).pipe(
map(risposta=>risposta),
tap(risposta => {
if(risposta === HttpEventType.Response) {
// do something
}
})
)

最新更新