如何在Angularfire2 Angular2中对FirebaseListObservable列表进行排序? [英] How can I sort a FirebaseListObservable list in Angularfire2 Angular2?

查看:219
本文介绍了如何在Angularfire2 Angular2中对FirebaseListObservable列表进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Firebase实时数据库中获取所有用户,并通过分数属性对其进行排序。我有这个工作使用变量

用户:FirebaseListObservable<任何[]> ;;

但我得到错误:

 类型'Observable< any []>'不可分配给类型'FirebaseListObservable< any []> 。 
属性'_ref'在类型'Observable< any []>'中缺少。

如果我将变量更改为

users: Observable< any []> ;;

然后错误消失,但是我不能使用像 .push()这样的Angularfire方法 .update()



我的抓取和排序代码如:

  this.users = this.af.database.list('/ users')
.map( items => items.sort((a,b)=> b.score - a.score));

我将不胜感激任何关于如何避免错误或任何首选方式的建议,谢谢!

解决方案

FirebaseListObservable implements lift ,所以RxJS运算符(如 map )将返回一个 FirebaseListObservable 实例。 p>

就是这样 - 当使用TypeScript时,RxJS运算符是静态类型返回 Observable ,所以你必须转换为 FirebaseListObservable 。 (使用RxJS不必涉及TypeScript,如果你看添加运算符的指导,您将会看到类型没有提及。)

您可以验证<通过查找 push update 函数返回code> FirebaseListObservable 你在你的问题中提到:

$ p $ this.users = this.af.database
.list('/ users' )
.map(items => items.sort((a,b)=> b.score - a.score))as FirebaseListObservable< any []>;
console.log(this.users.push.toString());
console.log(this.users.update.toString());

但是,请注意,这对 FirebaseListObservable 创建为查询的实例。有关此类实例的尚未解答的问题


I want to get all users from a Firebase realtime database and sort them by a score property. I've got this to work using the variable
users: FirebaseListObservable<any[]>;
but I get the errors:

Type 'Observable<any[]>' is not assignable to type 'FirebaseListObservable<any[]>'.
Property '_ref' is missing in type 'Observable<any[]>'.

If I change the variable to
users: Observable<any[]>;
then the errors go away, but then I can't use the Angularfire methods like .push() and .update().

My fetching and sorting code (which works) looks like:

this.users = this.af.database.list('/users')
  .map(items => items.sort((a, b) => b.score - a.score));

I would appreciate any advice on how this should be done to avoid the errors, or any preferred way, thank you!

解决方案

The FirebaseListObservable implements lift, so RxJS operators - like map - will return a FirebaseListObservable instance.

It's just that - when using TypeScript - the RxJS operators are statically typed to return Observable, so you have to cast to FirebaseListObservable. (Using RxJS does not have to involve TypeScript and if you look at the guidance for adding operators you will see that types are not mentioned.)

You can verify that a FirebaseListObservable instance is returned by looking for the push and update functions you mentioned in your question:

this.users = this.af.database
  .list('/users')
  .map(items => items.sort((a, b) => b.score - a.score)) as FirebaseListObservable<any[]>;
console.log(this.users.push.toString());
console.log(this.users.update.toString());

Note, however, that this will not work for FirebaseListObservable instances that are created as queries. There is an as-yet-unanswered question regarding such instances.

这篇关于如何在Angularfire2 Angular2中对FirebaseListObservable列表进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆