如何在FirebaseListObservable上使用.take? [英] How do I use .take on FirebaseListObservable?

查看:78
本文介绍了如何在FirebaseListObservable上使用.take?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我尝试在FireBaseListObservable上使用.take,.skip等RxJS运算符,则会出现"take is not a function"错误:

If I try to use RxJS operators like .take, .skip, etc. on FireBaseListObservable, I get "take is not a function" error:

import {Component} from '@angular/core';
import {AngularFire, FirebaseListObservable} from 'angularfire2';
import {Observable} from 'rxjs';

export class AppComponent {
  items: FirebaseListObservable<any>;

  constructor(public af: AngularFire) {
    // this works
    Observable.interval(1000).take(5).subscribe(x => console.log(x));

    this.items = af.database.list('/items');
    // this does not
    this.items.take(1).subscribe();
  }
}

通过导入"rxjs/add/operator/take"导入.take;确实有效,尽管现在我还有另一个问题:

importing .take via import "rxjs/add/operator/take"; did work, though now I have another question:

为什么

Observable.interval(1000).take(5).subscribe(x => console.log(x));

即使不导入汇票也能正常工作吗?

works even without importing the take?

该如何将Observable转换为FirebaseListObservable?

And how do I cast Observable to FirebaseListObservable?

推荐答案

RxJS的分发方式允许将其全部或小批量导入.

RxJS is distributed in a manner that allows it to be imported in its entirety or in small pieces.

要包含take运算符及其TypeScript声明,可以选择完全导入RxJS:

For the take operator and its TypeScript declarations to be included, you could choose to either import RxJS in its entirety:

import * as Rx from "rxjs/Rx";

或者可以仅导入所需的take运算符:

Or could import only the take operator that you require:

import "rxjs/add/operator/take";

请注意,AngularFire2可观察对象实现了 lift 以便与运算符进行合成.这样做的结果是,在使用运算符后,类型将为Observable<T>而不是FirebaseListObservable<T>,因此,如果将组合的可观察对象分配给FirebaseListObservable<T>变量或属性,则需要进行强制转换.

Note that the AngularFire2 observables implement lift for composition with operators. The effect of this is that after you use an operator, the type will be Observable<T> rather than FirebaseListObservable<T>, so if you are assigning the composed observable to a FirebaseListObservable<T> variable or property you will need a cast.

例如:

let item = af.database.list('/items').take(1) as FirebaseListObservable<any>;

但是,仅当您打算将该变量用作FirebaseListObservable(它具有其他方法)时,才需要这样做.通常,您将其保留为Observable. (我提到这一点是因为您拥有类型为FirebaseListObservable<any>的属性,并且我已经看到这会引起其他一些问题的混乱.)

However, you would only need to do so if you intended to use the variable as a FirebaseListObservable (it has additional methods). Typically, you'd leave it as an Observable. (I mentioned this as you had a property with the type FirebaseListObservable<any> and I've seen this cause confusion in a number of other questions.)

关于您在对该问题的评论中提到的错误,导入之间的相互作用很重要.如果要像这样导入Observable:

In relation to the errors you've mentioned in comments made against the question, the interplay between imports matters. If you are importing Observable like this:

import { Observable } from "rxjs";

您将完整地包含RxJS,并且在编写FirebaseListObservable实例时所有操作符都应可用.

You will be including RxJS in its entirety and all of the operators should be available when composing FirebaseListObservable instances.

但是,如果导入的Observable在导入模块中未在任何地方使用 ,则会被忽略,并且您将不会获得导入的任何内容.这可能就是为什么您需要为take显式导入的原因.

However, the if the imported Observable is not used anywhere in the module into which it is imported, it's ignored, and you won't get anything imported. Which is likely why you need the explicit import for take.

这篇关于如何在FirebaseListObservable上使用.take?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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