[英]Promise inside Observable in Angular 7+
我有一個我無法解決的問題。
在ngOnInit
事件中,我觀察到 url 參數。 此參數對應於firebase-storage中的文件夾。 這樣,在加載時,我會在該文件夾中獲得一個文件夾和/或文件列表,該列表被通知並將其存儲在類型為 Reference [] 的listReferences
變量中。
這是代碼:
ngOnInit() {
this.route.params
.subscribe(params => {
this.getFiles(params.ref).subscribe(
(listReferences) => {
this.listReferences = listReferences;
}
);
}
);
}
getFiles(folder: string) {
return this.storage.ref('/' + folder).listAll()
.pipe(
map((data) => {
return data.items;
})
);
}
事實證明,對於listReferences
數組中的每個項目,我需要訪問有希望的getDownloadUrl()
或getMetadata()
方法,並且我無法檢索數組中每個項目的值。 在這種情況下我應該如何處理? 如何最好地做到這一點?
基本上我遵循參考指南中包含的信息。 https://firebase.google.com/docs/reference/js/firebase.storage.Reference
如何使用ForkJoin
如下:
ngOnInit() {
this.route.params
.pipe(
mergeMap(x => this.getFiles(x.ref)),
mergeMap((listReferences: { getDownloadUrl: () => Promise<string> }[]) => {
return forkJoin(listReferences.map(x => x.getDownloadUrl()))
})
)
.subscribe(x => this.urls = x)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.