Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get "Observable.of([]);" to work?

What is the correct expression and import for Observable.of([]);.

import { of } from 'rxjs'; does not work for me.

like image 404
Rose Nettoyeur Avatar asked May 07 '18 16:05

Rose Nettoyeur


People also ask

How does an Observable work?

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.

How do you run an Observable?

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.

What is of () in RxJS?

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.


1 Answers

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

like image 105
martin Avatar answered Sep 22 '22 15:09

martin