rxjs observable pipe code example
Example 1: rxjs create observable from value
// Requires RXJS 6+
// Create an observable of any Type
// Ask yourself if the function creates a new observable or not
// If it creates a new one then it is imported from 'rxjs'
// Operators are imported from 'rxjs/operators'
import { of } from 'rxjs';
// T => Observable
const value$ = of(1);
Example 2: angular observable pipe exemple
import { Component, OnInit } from '@angular/core';
import { Observable, of} from 'rxjs';
import { map, filter, tap } from 'rxjs/operators'
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
obs = new Observable((observer) => {
observer.next(1)
observer.next(2)
observer.next(3)
observer.next(4)
observer.next(5)
observer.complete()
}).pipe(
filter(data => data > 2), //filter Operator
map((val) => {return val as number * 2}), //map operator
)
data = [];
ngOnInit() {
this.obs1.subscribe(
val => {
console.log(this.data)
}
)
}
}