RxJS 6 examples in Angular 6 – Creating observables

Today I’m starting a new series of articles looking at observables in Angular 6 and RxJS 6. Observables is a big subject so I’m going to keep coming back to it.

“RxJS has been rearranged to make it more tree-shakable, ensuring that only the pieces of RxJS that you use are included in your production bundles.” , blog.angular.io. You can learn more about this here.

The following examples follow the new Angular 6 coding styles. They are tree-shakable and future-proof.

of = create an observable from a sequence

The “of” method creates an observable from any sequence. e.g. of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).

Try it out on StackBlitz.

 

from = convert an array into an observable

The “from” method creates an observable from an array. e.g. from(array).

Try it out on StackBlitz.

 

interval = emit a value every second

The “interval” operator emits a value every second. e.g. interval(1000).

Try it out on StackBlitz.

 

fromEvent = capture an event e.g. a keypress

The “fromEvent” method creates an observable from an event. E.g. fromEvent(myInput, ‘keyup‘).

Try it out on StackBlitz.

 

Other articles in this series

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.