正在获取Angular 2+的Firebase Storage的下载URL


ref: AngularFireStorageReference;
task: AngularFireUploadTask;
uploadState: Observable<string>;
uploadProgress: Observable<number>;
downloadURL: Observable<string>;
upload(event) {
const id = Math.random().toString(36).substring(2);
this.ref = this.afStorage.ref(id);
this.task = this.ref.put(event.target.files[0]);
this.uploadState = this.task.snapshotChanges().pipe(map(s => s.state));
this.uploadProgress = this.task.percentageChanges();
this.downloadURL = this.task.downloadURL();
}

以上来自指南。

我希望在上传完成到firebase存储后,它能获得下载URL。但我得到了这个错误:

TS2740:类型"Subscription"在类型"Observable"中缺少以下属性:_isScalar、source、operator、lift和6个其他属性。

解决方案:

upload(event) {
const id = Math.random().toString(36).substring(2);
const file = event.target.files[0];
const filePath = id;
this.ref = this.afStorage.ref(id);
this.task = this.afStorage.upload(filePath, file);
this.uploadState = this.task.snapshotChanges().pipe(map(s => s.state));
this.uploadProgress = this.task.percentageChanges();
this.task.snapshotChanges().pipe(
finalize(() => this.downloadURL = this.ref.getDownloadURL() )
).subscribe()
}

最新更新