角 – 叉连接两个火基可观测量
我正在使用angular2fire.我正在查询并试图从一个城市获得所有的旅行.
getAllTours(cityId) { return this.af.database.list(`/cities/${cityId}/tours`) .map((tours): any => { tours.map((tour: any) => { tour.tour = this.af.database.object(`/tours/${tour.$key}/tours`) }); return tours; }) } 如果我在console.log中使用了tour对象,我会得到一个“FirebaSEObjectObservable”数组. 我必须循环遍历所有FirebaSEObjectObservable,以获取实际数据. 我想知道我是否可以forkJoin所有的observable并将输出作为一个具有单个订阅函数的数组. 这是一种正确的方法吗? 我知道我可以在所有观察者数组上执行异步管道,但我想在控制器内部获取数据,然后在视图中显示之前进行一些处理,因此异步管道对我来说真的不是最好的解决方案. 解决方法
是的,forkJoin可用于获取内部可观察数据的数据:
getAllTours (cityId) { return this.af.database .list(`/cities/${cityId}/tours`) .mergeMap((tours) => { // The array of tours is going to be mapped to an observable,// so mergeMap is used. return Observable.forkJoin( // Map the tours to the array of observables that are to // be joined. Note that forkJoin requires the observables // to complete,so first is used. tours.map((tour) => this.af.database .object(`/tours/${tour.$key}/tours`) .first() ),// Use forkJoin's results selector to match up the result // values with the tours. (...values) => { tours.forEach((tour,index) => { tour.tour = values[index]; }); return tours; } ); }); } 是否使用forkJoin是正确的方法将取决于您的要求. 使用上面的代码,getAllTours返回的observable将不会发出一个值,直到所有内部observable都已完成 – 也就是说,直到查找了每个城市的游览.这可能会影响感知性能 – 如果在/tours/${tour.$key}/tours中的信息被查找之前可以显示/ cities / ${cityId} / tour中的信息,您将无法查看展示它.同样,当结果到达时,您将无法显示城市的游览. 使用forkJoin可以简化处理实现,但可能会让UI感觉更慢. (但是,对UI的零碎更新可能是您不想要的.) 请注意,如果您确实需要在视图中显示每个城市的游览之前对其进行一些处理,您可以在问题的代码中对可观察对象执行所述处理.例如,使用getAllTours函数: observable = getAllTours(someCityId); observable.map((tours) => { tours.forEach((tour) => { // With your function,tour.tour is an observable,so map // could be used to process the values. tour.tour = tour.tour.map((value) => { // Do some processing here with the value. }) // And,if you are not interested in dynamic updates,you could // call first. .first(); }); return tours; }); 然后,您可以在模板中使用异步管道,它将接收您处理的巡视路线. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |