[英]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.