In this tutorial, we will explore how to use Reactive Extensions for JavaScript (RxJS) in Angular for managing state and handling data streams.
You will learn about Observables, a key component of RxJS, and how to use them to manage asynchronous data streams. This will include creating, subscribing to, and manipulating Observables.
Observable.create()
or new Observable()
methods.Here's a basic example of creating and subscribing to an Observable:
import { Observable } from 'rxjs';
let observable = new Observable(subscriber => {
subscriber.next('Hello');
subscriber.next('World');
subscriber.complete();
});
observable.subscribe(
value => console.log(value),
err => {},
() => console.log('This is the end')
);
In the above example, we first import the Observable from 'rxjs'. We then create an Observable that emits 'Hello', 'World', and then completes. We subscribe to this Observable and log each value to the console.
map()
, filter()
, concat()
, and mergeMap()
.Here's an example of using the map()
operator:
import { from } from 'rxjs';
import { map } from 'rxjs/operators';
let numbers = from([1, 2, 3, 4, 5]);
let doubleNumbers = numbers.pipe(map(value => value * 2));
doubleNumbers.subscribe(value => console.log(value));
In the above example, we first create an Observable from an array of numbers. We then use the pipe()
function to chain operators, in this case, the map()
operator is used to double the value of each number. Finally, we subscribe to the Observable and log each value to the console.
import { Observable } from 'rxjs';
// Create an Observable that will start listening to data when subscribed to.
let observable = new Observable(subscriber => {
// Emit a single string value
subscriber.next('Hello');
// Emit another value
subscriber.next('World');
// Complete the observable stream
subscriber.complete();
});
// Subscribe to the observable
observable.subscribe(
value => console.log(value), // Handle the data within the stream
err => {}, // Handle error
() => console.log('This is the end') // Handle completion
);
In this example, you should see 'Hello', 'World', and 'This is the end' logged to your console.
import { from } from 'rxjs';
import { map } from 'rxjs/operators';
// Create an observable from an array
let numbers = from([1, 2, 3, 4, 5]);
// Use the map operator to manipulate the data
let doubleNumbers = numbers.pipe(map(value => value * 2));
// Subscribe to the observable
doubleNumbers.subscribe(value => console.log(value));
In this example, you should see the numbers 2, 4, 6, 8, and 10 logged to your console.
We've covered the basics of using RxJS for state management and data streams in Angular. We've learned about Observables and Operators, and how to use them to handle asynchronous data streams.
Continue practicing with Observables and Operators. Try creating your own Observables and manipulating them with different Operators.
import { of } from 'rxjs';
let numbers = of(1, 2, 3, 4, 5);
numbers.subscribe(value => console.log(value));
import { from } from 'rxjs';
import { filter } from 'rxjs/operators';
let numbers = from([1, 2, 3, 4, 5]);
let evenNumbers = numbers.pipe(filter(value => value % 2 === 0));
evenNumbers.subscribe(value => console.log(value));
import { interval } from 'rxjs';
let seconds = interval(1000);
seconds.subscribe(value => console.log(value));
In this exercise, you should see a new number logged to your console every second.