在引用最新的rxjs时获取rxjs错误
getting rxjs errors when referencing latest rxjs
我正在使用本教程https://egghead.io/lessons/rxjs-creating-an-observable它引用了2.5.2 rxjs版本。
我引用的是rxjs@5.0.0-beta.6"
npm包<script src="node_modules/rxjs/bundles/rx.umd.js"></script>
中的最新rx.umd.js
这是我试图运行的代码:
console.clear();
var source = Rx.Observable.create(function(observer){
setTimeout(function() {
console.log('timeout hit');
observer.onNext(42);
observer.onCompleted();
}, 1000);
console.log('started');
});
var sub = source.subscribe(function(x) {
console.log('next ' + x);
}, function(err) {
console.error(err);
}, function() {
console.info('done');
});
setTimeout(function() {
sub.dispose()
}, 500);
这是我得到的控制台输出。
Console was cleared
script.js:10 started
script.js:22 Uncaught TypeError: sub.dispose is not a function
script.js:5 timeout hit
script.js:6 Uncaught TypeError: observer.onNext is not a function
plunker:https://plnkr.co/edit/w1ZJL64b8rnA92PVuEDF?p=catalogue
rxjs 5 api是否与rxjs 2.5有很大不同,并且不再支持observer.onNext(42);
和sub.dispose()
?
更新2018/12:
RxJS v6.x引入了一种新的、更具"功能性"的API。有关更多信息,请参阅5>6迁移指南。原始示例代码仍然有效,但您必须导入of
运算符,如下所示:
// ESM
import { of } from 'rxjs'
// CJS
const { of } = require('rxjs');
原始RxJS 5答案:
没错。RxJS 5被重写以提高性能,同时也符合ES7 Observable
规范。查看Github上的RxJS 4->5迁移页面。
下面是一个工作示例:
// Create new observable
const one = Observable.of(1,2,3);
// Subscribe to it
const oneSubscription = one.subscribe({
next: x => console.log(x),
error: e => console.error(e),
complete: () => console.log('complete')
});
// "Dispose"/unsubscribe from it
oneSubscription.unsubscribe();
很多方法都被重命名了,但API本身很容易转换到。
不确定这是否能帮助到其他人,但我在这里遇到了一个类似的错误:
old.dispose is not a function
在我的案例中,问题是我将一些旧的rxjs与来自新版本rxjs的可观测性混合在一起。
因此,我通过更新所有调用以使用最新的rxjs来解决问题。
相关文章:
- Node.js v6.2.0类扩展不是函数错误
- Jquery菜单操作不稳定,定位不正确,存在一般错误
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- 试图在引导模式内动态生成图表,得到offsetWidth错误
- 为什么会出现错误;未捕获的类型错误:undefined不是函数;
- 我如何修复包含在captcha的addthis中的错误
- 同样,同样的错误'ahorcado.js:26未捕获类型错误:无法读取属性'beginPath'
- 节点是否需要模块传递带有方括号的arg?这是个错误吗
- Webpack/Rect:遵循egghead.io教程,但出现错误:您可能需要一个合适的加载程序来处理此文件类型
- CKFinder 3为所选文件返回错误的URL
- 如何仅在RxJs中可观察到的源发出的特定错误上重试
- RxJS:在循环中处理错误.js自定义驱动程序
- 最后,在 rxjs 序列上,在第一个错误时执行
- 如何捕获请求中的错误,然后打开模态,然后在模态使用RxJS关闭时重试
- 在引用最新的rxjs时获取rxjs错误
- RXJS 中的分页数据光标以及对 subject.onDone 和错误的混淆
- 错误在RxJs中被认为是来自可观察对象的发射吗?
- 如何使用Angular处理RxJS中的错误
- RxJS 错误处理
- Rxjs吞下错误