【发布时间】:2018-11-02 23:19:32
【问题描述】:
在 Angular 6 中工作我有两个 HTTP 调用需要串联执行。第一次调用成功,第二次调用表示成功,但从未真正发出 HTTP 请求。
如果我分解这两个调用并单独执行它们,它们都可以工作。但是,当将它们串联组合时,第二个调用永远不会起作用。
这里的一般想法是从服务器请求一个签名的 URL,并在收到后将文件上传到指定的 URL。
export class StaticAssetService {
constructor(private httpClient: HttpClient) { }
public uploadAsset(assetType: StaticAssetType, file: File): Observable<any> {
if (file) {
return this.httpClient.get(`${environment.apiURI}/secure/file-upload/getPresignedURLForUpload.json`, {
params: {
assetType: assetType,
originalFileName: file.name
}
}).pipe(map(response => {
return this.httpClient.put(response.signedURL, file, {
headers: new HttpHeaders({'Content-Type': file.type}),
reportProgress: true
})
}));
}
}
}
【问题讨论】:
-
停止使用any,编译器会告诉你问题出在哪里。用 switchMap 替换地图。
标签: angular http nested rxjs angular6