【问题标题】:pluck not found on Rx.observable在 Rx.observable 上找不到采摘
【发布时间】:2016-01-01 23:52:52
【问题描述】:

我正在玩 https://github.com/Reactive-Extensions/RxJS 尝试使用 Typescript 和 Angular 2 运行他们的示例。当我编译代码时,它抱怨“属性 'pluck' 在类型 'Observable' 上不存在” .我不明白为什么 pluck 不可用。 rxjs 5 是否已将其删除?

使用:

角度 2.0.0-beta.0 rxjs 5.0.0-beta.0

代码:

import {Component} from 'angular2/core';
import {FORM_DIRECTIVES} from 'angular2/common';
import {Http} from 'angular2/http';
import {Observable} from 'rxjs/Observable';

import {Title} from '../providers/title';

@Component({
  selector: 'home',
  directives: [...FORM_DIRECTIVES],
  providers: [Title],
  pipes: [],
  styles: [require('./home.css')],
  template: require('./home.html')
})
export class Home {
  wikisearch: any = {};

  // TypeScript public modifiers
  constructor(public title: Title, public http: Http) {
    var input = document.getElementById('wikisearch');

    /* Only get the value from each key up */
    var keyups = Observable.fromEvent(input, 'keyup')
      .pluck('target', 'value')
      .filter((text) => text.length > 2);
  }
}

【问题讨论】:

  • 我认为你应该有类似Observable.fromEvent<TYPE[]>() 的东西,其中TYPE 是你从该函数中获得的项目类型。
  • 快速解决方案是Observable.fromEvent<any[]>(...)
  • @Louy 这无济于事。这不是打字稿问题。问题是 pluck 还没有实现。

标签: angular typescript rxjs


【解决方案1】:

Angular 2 使用 RxJS 5 (https://github.com/ReactiveX/RxJS),这是对当前版本 (https://github.com/Reactive-Extensions/RxJS) 的完全重写。 RxJS 5 仍处于测试阶段。 RxJS 4 中的一些运算符在 RxJS 5 中尚不可用。pluck 就是这种情况。我建议您查看RxJS 5 documentationmigration guide,因为这两个版本之间存在一些 API 差异。

对于这个特定示例,您可以使用 map 轻松替换 pluck 运算符:

var keyups = Observable.fromEvent(input, 'keyup')
    .map((target) => target.value)
    .filter((text) => text.length > 2);

更新: pluck 在 RxJS 5 中是 now available,因为版本 5.0.0-beta.1

【讨论】:

  • 感谢@luisgabriel,这与我得出的结论相同。我遇到的问题是我正在查看旧文档,但找不到测试版的文档。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-26
  • 1970-01-01
  • 2013-05-30
  • 2012-12-07
相关资源
最近更新 更多