我的棱角应用程序我想以Blob
格式下载pdf并进行处理以获取Uint8Array
。我想在服务中执行此操作,但是我无法返回rxjs Observable<Uint8Array>
。我写以下代码
public loadBinaryPDF(url): Observable<Uint8Array> {
return this.httpClient.get(url, { responseType: 'blob' }).pipe(map(async blob => {
let arrayBuff = await new Response(blob).arrayBuffer(); // some await here..
return new Uint8Array(arrayBuff);
}));
}
但是出现编译错误
ERROR in src/services/api-companies.service.ts(454,9): error TS2322:
Type 'Observable<Promise<Uint8Array>>' is not assignable to type 'Observable<Uint8Array>'.
Type 'Promise<Uint8Array>' is missing the following properties from type 'Uint8Array':
BYTES_PER_ELEMENT, buffer, byteLength, byteOffset, and 25 more.
所以问题是我在async-await
/ pipe
中使用了map
。 如何解决它?(我不会找到允许使用async-await并返回observable的方式)
答案 0 :(得分:1)
您可以仅用作switchMap
而不是地图。
public loadBinaryPDF(url): Observable<Uint8Array> {
return this.httpClient.get(url, { responseType: 'blob' }).pipe(switchMap(async blob => {
let arrayBuff = await new Response(blob).arrayBuffer();
return new Uint8Array(arrayBuff);
}));
}