Import.of () for observation in typescript

I use this great repo for my test project angular 2 (TypeScript) - https://github.com/qdouble/angular-webpack2-starter . And I need to use Observable.of (..) . When I try to import it:

import { Observable } from "rxjs/Observable"; import { of } from 'rxjs/observable/of'; 

I get:

The 'of' property does not exist in typeof Observable 'type.

I also tried it as follows:

 import { Observable } from "rxjs/Observable"; import { of } from 'rxjs/add/observable/of'; // notice 'add' 

I got:

node_modules / rxjs / add / observable / of "'does not have an exported member'.

So how can I import this static () static method for Observable ???

+5
source share
1 answer

You do not need to import {of} from 'rxjs/add/observable/of' . You can directly use

 import { Observable } from "rxjs/Observable"; import "rxjs/add/observable/of"; 

Or you can import an Observable from "rxjs / Rx", which binds all operators. Bad practice

 import { Observable } from "rxjs/Rx"; 

Update 2018-01-26: RxJS v5.5 + operators with protocol

From https://github.com/ReactiveX/rxjs/blob/master/doc/pipeable-operators.md

Starting with version 5.5, we sent "tubular operators" that can be accessed in rxjs / operators (note the pluralized "operators"). They are designed to provide a better approach for attracting only the operators you need than the patch operators found in rxjs / add / operator / *.

Now that the import “fix” is deprecated, it would be better to use strict import.

 import { of as observableOf } from 'rxjs/observable/of' 

and use it that way

 const myObs$: Observable<number> = observableOf(1, 2, 3) 
+11
source

Source: https://habr.com/ru/post/1259695/


All Articles