具有 RX.Js 5.0.0 拉取 'Not Defined' 错误的 JSBIN 项目

JSBIN Project with RX.Js 5.0.0 Pulling 'Not Defined' Error

当我 运行 一个具有以下规范的新 JSBIN 项目时:

- 使用选项卡:JavaScript、控制台

- 添加库:RxJS 5.0.0

然后在JavaScript区运行如下代码块:

var observable = Rx.Observable.create(observer => {
  setInterval(() => {
    observer.onNext('This is the output of my async operation');
  }, 2000);
});

observable.subscribe(response => console.log(response));

前面的代码应该在控制台区域呈现以下输出:

"This is the output of my async operation"

两秒后,控制台区域应该渲染:

"This is the output of my async operation"

但是,我收到以下错误:

"error"
-----------------------------
"ReferenceError: Rx is not defined
    at yivicazake.js:3:4
    at https://static.jsbin.com/js/prod/runner-3.39.12.min.js:1:13926
    at https://static.jsbin.com/js/prod/runner-3.39.12.min.js:1:10855

这是我第一次使用 JSBIN 将 RxJS 作为库引入,我希望有人遇到过这个特定错误。

我不确定你使用的是哪个版本的 Rxjs beta,我在这里创建了一个 jsbin,它对我来说工作正常 http://jsbin.com/henimevepa/edit?html,js,console,output

这里有一些东西

- instead of '.onNext' in version 5 its just '.next'
- You need to subscribe to observer to run it.

我也在学习RxJS,这里有几点需要注意。现在仍然非常容易混淆 RxJS v4 和 v5 文档,所以一些链接可以提供帮助:

v5 存储库是这个 https://github.com/ReactiveX/RxJS. The /Reactive-Extensions/RxJS is for v4. Both are still useful so if you're following courses online with v4 (there are lots of them), the migration docs 会有帮助!

这个manual is very helpful and RxMarbles也是。

至于你的代码,试试这个:

// create subscriber
const createSubscriber = tag => ({
    next(item) { console.log(`${tag}.next ${item}`); },
    error(error) { console.log(`${tag}.error ${error.stack || error}`); },
    complete() { console.log(`${tag}.complete`); }
});

// interval
Rx.Observable
    .interval(2000)
    .take(5)
    .subscribe(createSubscriber('This is the output of my async operation'));

希望对您有所帮助!