What is the correct expression and import for Observable.of([]);
.
import { of } from 'rxjs';
does not work for me.
The Observable is initialized with a function. This function is the observer . The observer gets called with an object, the subscriber . Once the observer function gets called with the subscriber object, it can call three methods in this subscriber — the next , error , and complete methods.
To execute the observable you have created and begin receiving notifications, you call its subscribe() method, passing an observer. This is a JavaScript object that defines the handlers for the notifications you receive.
RxJS' of() is a creational operator that allows you to create an RxJS Observable from a sequence of values. According to the official docs: of() converts the arguments to an observable sequence. In Angular, you can use the of() operator to implement many use cases.
Since RxJS 6 you should import all "creation" observables directly from 'rxjs'
(assuming you have path maps set when bundling your app).
More detailed explanation: https://github.com/ReactiveX/rxjs/blob/master/MIGRATION.md#import-paths
import { of } from 'rxjs';
of(1).subscribe(console.log);
See demo: https://stackblitz.com/edit/typescript-e3lxkb?file=index.ts
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With