2

当我运行具有以下规范的新 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 作为库引入,我希望有人对这个特殊错误有过经验。

4

2 回答 2

2

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

v5 存储库是这个https://github.com/ReactiveX/RxJS。/Reactive-Extensions/RxJS 适用于 v4。两者都仍然有用,因此如果您使用 v4 在线学习课程(其中有很多),迁移文档会有所帮助!

手册非常有用,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'));

希望这可以帮助!

于 2016-08-21T14:48:59.283 回答
2

我不确定你使用的是哪个确切版本的 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.
于 2016-08-21T07:39:58.803 回答