angular - mat-table 不会呈现我的 Observable使用mergeMap时
问题描述
在 stackBlitz 上发布了很好的示例 :-)
https://stackblitz.com/edit/angular-f351cx
我在将 firebase 中的两个数据库查找组合成一个可以很好地呈现到我的 mat-table 中的单个 observable 时遇到了一些麻烦。
我走得很远。我可以通过返回 observable 来获得一个数据库查找以显示在表中,但是一旦我尝试连接第二个,表中没有任何显示。
我觉得这很奇怪,因为当我将第二个 Observable 的结果进行异步/JSON 管道传输时,结果似乎具有正确的格式。
我究竟做错了什么?我的示例使用 http.get() 和 restcountries.eu,但我实际上是在尝试使其与 Firebase 一起使用。但是,在我自己的应用程序和本示例中,错误/行为是相同的。
解决方案
看起来当您return of(myArray)
在从第二个 http 请求中获取值之前调用 observable 完成。
如果添加.pipe(delay(100));
到它,则会显示表中的值。
代码将是:
this.singleObsDataSource = this.http.get('https://restcountries.eu/rest/v2/regionalbloc/efta')
this.doubleObsDataSource = this.singleObsDataSource.pipe(mergeMap((data: any[]) => {
let myArray = [];
data.forEach(country => {
let url = 'https://restcountries.eu/rest/v2/name/' + country['name'];
this.http.get(url).subscribe(countryInfo=>{
myArray.push(countryInfo[0]);
})
})
return of(myArray).pipe(delay(100));
}))
推荐阅读
- java - 将 Flyway 添加到已投入生产的项目中
- postgresql - 在 postgres 中匿名化 JSONB 的更好方法?
- c# - 总结在列表属性列表中
- reactjs - 在 useState 定义的对象中设置对象
- r - 为什么我在 R 中的摘要只包括我的一些变量?
- javascript - 使用 JavaScript 循环遍历复杂的 JSON
- python - 在 SQLAlchemy 中执行许多 dml 语句的最佳性能方式
- python - 如何使 setup.py 的 install_requires 部分跳过已安装的依赖项
- reactjs - ReactJs在TextInput中显示状态值
- jenkins - 詹金斯当条款没有按预期工作时