【发布时间】:2018-09-22 17:17:10
【问题描述】:
具体情况是这样的: AngularFirebase 文档说,为了从上传的文件中获取下载 URL,您需要这样做:
const task = this.storage.upload(filePath, file);
// observe percentage changes
// get notified when the download URL is available
task.snapshotChanges().pipe(
finalize(() => this.downloadURL = fileRef.getDownloadURL() )
)
.subscribe();
}
现在,我有一个上传方法,我想从它返回一个可观察的字符串。我的方法签名看起来像:
upload(file :File) :Observable<string> {
......
}
我试过这样做:
return <Observable<string>>task.snapshotChanges().pipe(
finalize(() => {
const url = fileRef.getDownloadURL();
console.log('download url is ',url);
return url;
})
);
但这不起作用,因为原始 observable 中还有其他类型的快照更改元素。
现在我的问题是,如何使用示例中的代码有效地返回我想要的类型
【问题讨论】:
-
只需删除
subscribe,然后返回您所拥有的 -
那行不通。我更新了问题
-
对不起,我的错。添加(在
finalize之后)map(() => fileRef.getDownloadURL())。不确定你从哪里得到fileRef,但map函数将转换 Observable 发出的值 -
触发次数过多。这工作stackblitz.com/edit/typescript-nqcr4w 但丑陋