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

查看:21
本文介绍了我如何在 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 observables 为 lift 实现了与运算符的组合.这样做的结果是,在您使用运算符后,类型将是 Observable 而不是 FirebaseListObservable,因此如果您将组合的 observable 分配给一个 FirebaseListObservable 变量或属性,您需要进行转换.

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 的属性,我已经看到这会在许多其他问题中造成混淆.)

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天全站免登陆