Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rxjs Subject next or onNext

Tags:

angular

rxjs

I'm quite new at rxjs stuff be patience. For example in this tutorial http://blog.angular-university.io/how-to-build-angular2-apps-using-rxjs-observable-data-services-pitfalls-to-avoid/ but I saw the same code in the ng-book

I can see

let subject = new Rx.Subject();
subject.subscribe(value => console.log('Received new subject value: '))
subject.next(newValue);

but if I put the code in the browser I've got

subject.next is not a function

so if I take a look to the doc https://github.com/Reactive-Extensions/RxJS/blob/master/doc/gettingstarted/subjects.md

var subject = new Rx.Subject();
subject.subscribe(value => console.log('Received new subject value: ',value))
subject.onNext(2);

Can you explain me why the tutorial and the book are using next ? What am I missing ?

like image 645
Whisher Avatar asked Jul 24 '16 19:07

Whisher


People also ask

Why use subject instead of observable?

In comparison to a regular Observable, a Subject allows values to be multicasted to many Observers. A Subject and its subscribers have a one-to-many relationship. A Subject can be an Observable as well as an Observer. They hold a registry of many listeners to multiple Observables.

When should I use ReplaySubject?

Use a ReplaySubject when you need more than the last given value. (For example, the previous five values) Or you want to set a time window for the values can be validly sent to subscribers. Use an AsyncSubject when you only want the last value to be passed to the subscribers.

What does next do in RxJS?

There are three types of values an Observable Execution can deliver: "Next" notification: sends a value such as a Number, a String, an Object, etc. "Error" notification: sends a JavaScript Error or exception. "Complete" notification: does not send a value.

What is Observer next?

Observer. Observer can be an Object with An object with next , error and complete methods on it and it can be passed to observable. subscribe(observer) method. Observable will call the Observer's next(value) method to provide data. And similarly calls Observer's error(err) or Observer's complete() methods once.


1 Answers

Seems you're using the wrong rxjs version (4.x). Angular2 uses rxjs 5 https://github.com/ReactiveX/rxjs/

See also http://reactivex.io/rxjs/

like image 79
Günter Zöchbauer Avatar answered Sep 28 '22 10:09

Günter Zöchbauer