2016-04-14 17 views
7

Sto utilizzando questo tutorial https://egghead.io/lessons/rxjs-creating-an-observable che fa riferimento alla versione 2.5.2 di rxjs.ottenere errori rxjs quando si fa riferimento all'ultima versione rxjs

sto riferimento a l'ultimo rx.umd.js da [email protected]" pacchetto NPM <script src="node_modules/rxjs/bundles/rx.umd.js"></script> E qui è il codice che sto cercando di eseguire:

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); 

Ecco un output della console sto ottenendo.

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

Is rxjs 5 api è molto diverso da rxjs 2.5 e observer.onNext(42); e sub.dispose() non è supportato più a lungo?

risposta

7

Proprio così. RxJS 5 è stato riscritto per migliorare le prestazioni e anche conforme alla specifica ES7 Observable. Controlla lo RxJS 4->5 migration page su Github.

Ecco un esempio di lavoro:

// 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(); 

Un sacco di metodi ottenuto rinominato, ma l'API in sé è molto facile per la transizione al.

+0

https://github.com/ReactiveX/rxjs/blob/master/MIGRATION.md#observer-interface-changes-also-subjects – Cody