在引用最新的rxjs时得到rxjs错误

sre*_*moh 9 javascript rxjs rxjs5

我正在使用本教程https://egghead.io/lessons/rxjs-creating-an-observable,它引用了2.5.2 rxjs版本.

我引用了最新rx.umd.jsrxjs@5.0.0-beta.6"NPM包<script src="node_modules/rxjs/bundles/rx.umd.js"></script> 这里是我试图运行的代码:

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);
Run Code Online (Sandbox Code Playgroud)

这是我得到的控制台输出.

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
Run Code Online (Sandbox Code Playgroud)

plunker:https://plnkr.co/edit/w1ZJL64b8rnA92PVuEDF p = catalogue

rxjs 5 api与rxjs 2.5有很大不同吗?observer.onNext(42);并且sub.dispose()不再支持?

eld*_*dov 9

更新2018/12:

RxJS v6.x引入了一个新的,更"功能"的API.请查看5> 6迁移指南以获取更多信息.原始示例代码仍然有效,但您必须of像这样导入运算符:

// ESM
import { of } from 'rxjs'
// CJS
const { of } = require('rxjs');
Run Code Online (Sandbox Code Playgroud)

原创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();
Run Code Online (Sandbox Code Playgroud)

很多方法都被重命名,但API本身很容易过渡到.